Priority Queue Implementation

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 ??
LVL 20
Who is Participating?
hoomanvConnect With a Mentor Commented:
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;
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

a_bAuthor Commented:
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 ??
a_bAuthor Commented:
Is it possible to sort a hashset ??
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.