Solved

Priority Queue Implementation

Posted on 2006-10-26
6
440 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
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
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

How our DevOps Team Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Application launch issue with Apache Tomcat 5 45
stackato and cloud 4 87
eclipse shortcuts 9 58
jboss wildfly 10.1 10 110
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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.

825 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