Solved

Priority Queue Implementation

Posted on 2006-10-26
6
434 Views
Last Modified: 2008-01-09
I have to maitain a priority queue in java.

I have a string and a cost associated with the string, the priority queue has to be sorted according to the cost. I am sout suer on how to implement this using the PrirityQueue Class within java.
I am guessing that I have to stroe each object as a Double Dimenstional String where the first location specified the string cost and the second location specifies the string itself. How do I specify to ProiorityQueue to sort the queue by the cost ??

Any suggestions on implementation ??
0
Comment
Question by:a_b
  • 2
  • 2
  • 2
6 Comments
 
LVL 92

Accepted Solution

by:
objects earned 300 total points
ID: 17815567
0
 
LVL 92

Assisted Solution

by:objects
objects earned 300 total points
ID: 17815569
0
 
LVL 14

Assisted Solution

by:hoomanv
hoomanv earned 200 total points
ID: 17817458
Pass objects of this type to PriorityQueue

class StringCost implements Comparable<StringCost> {
    String str;
    int cost;
    public int compareTo(StringCost o) {
        return cost - o.cost;
    }
}
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 20

Author Comment

by:a_b
ID: 17828746
I implemented the Comparator but the problem is that it is taking too long during execution. Any suggestions on how to improve the execution time ?? Do I use HashSet ?? Also how do I use HashSet for a Stack ??
0
 
LVL 20

Author Comment

by:a_b
ID: 17830466
Is it possible to sort a hashset ??
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 17831719
HashSet makes no guarantees that the iteration order remains constant over time
Either use LinkedHashSet and put entries in sorted order
Or extract them into a List and sort that list
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now