Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Priority Queue Implementation

Posted on 2006-10-26
6
Medium Priority
?
460 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 92

Accepted Solution

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

Assisted Solution

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

Assisted Solution

by:hoomanv
hoomanv earned 600 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This video teaches viewers about errors in exception handling.
Suggested Courses

721 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