Solved

Priority Queue Implementation

Posted on 2006-10-26
6
431 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

757 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

21 Experts available now in Live!

Get 1:1 Help Now