Priority queue in Java?

Yes PriorityQueue has a constructor that allows you to a pass a Comparator to define the order of the elements. For example, if you have the following Bar class: public class Bar { private int priority; // getters / setters ... } And you want to create a priority queue that orders the elements based on the priority field (e.g. , items with greater priority stay in the front of the queue), you can use the following: Queue queue = new PriorityQueue(new Comparator() { public int compare(Bar a1, Bar a2) { return a2.getPriority() - a1.getPriority(); // adapt this to your needs } }) If you have more complex logic in the compare method, or if you want to reutilize the code, then I suggest you create a class, say BarComparator that implements ComparatorPriority; } } Hope it helps.

Yes, PriorityQueue has a constructor that allows you to a pass a Comparator to define the order of the elements. For example, if you have the following Bar class: public class Bar { private int priority; // getters / setters ... } And you want to create a priority queue that orders the elements based on the priority field (e.g. , items with greater priority stay in the front of the queue), you can use the following: Queue queue = new PriorityQueue(new Comparator() { public int compare(Bar a1, Bar a2) { return a2.getPriority() - a1.getPriority(); // adapt this to your needs } }); If you have more complex logic in the compare method, or if you want to reutilize the code, then I suggest you create a class, say BarComparator, that implements Comparator. Also, as an alternative to the above, you can make Bar implement the Comparable interface, and use the empty constructor, like so: public class Bar implements Comparable { private int priority; @Override public int compareTo(Bar b) { return b.getPriority() - this.

Priority; } } Hope it helps.

When you say ordering by greater priority do you mean in the sense that a Bar with priority 3 has a greater priority then a Bar with priority 13, in the sense that Bar's that have a smaller priority value actually have a "greater priority" – Tomek Sep 28 '09 at 14:59 The exact opposite. A Bar a with a priority 20 has a greater priority than a Bar be with priority 18, and thus, Bar a stays in the front of the queue. – João Sep 28 '09 at 19:54.

Priority queue will be ordered according to the same ordering. Natural ordering of its elements. Specified priority queue.

Specified sorted set. According to the same ordering as the given sorted set. Inserts the specified element into this priority queue.

Returns:true (as specified by Collection. Inserts the specified element into this priority queue. Returns:true (as specified by Queue.

Or returns null if this queue is empty. If it is present. Result of the call).

Returns true if this queue contains the specified element. At least one element e such that o. Returns an array containing all of the elements in this queue.

The elements are in no particular order. Maintained by this queue. A new array).

The caller is thus free to modify the returned array.

There are two ways to do this. Either way, you want to create a custom object that holds both the String (the value you want) and the integer (the priority) The first solution is to have this data object implement Comparable : class Data implements Comparable { private final String message; private final int priority; public Data(String message, int priority) { this. Message = message; this.

Priority = priority; } @Override int compareTo(Data other) { return Integer. ValueOf(priority). CompareTo(other.

Priority); } // also implement equals() and hashCode() } Then when you do PriorityQueue queue = new PriorityQueue() the queue will order items by the order defined by the compareTo method The problem with this solution is that if you want the ordering to be only on the integer, then either equals method and your compareTo method will not be consistent or your equals method will not be correct A better solution would be to use the PriorityQueue constructor that takes a Comparator In this case Data wouldn't have to implement Comparable you just need a Comparator that defines your ordering: public final class OrderDataByPriority implements Comparator { public static final OrderDataByPriority INSTANCE = new OrderDataByPriority(); private OrderDataByPriority() {} @Override public int compare(Data data1, Data data2) { return Integer. ValueOf(data1. Priority).

CompareTo(data2. Priority); } @Override public boolean equals(Object other) { return other == OrderDataByInteger. INSTANCE; } private Object readResolve() { return INSTANCE; } } Note that since this comparator takes no data, I made it a singleton You can then create the queue line this: PriorityQueue queue = new PriorityQueue( initialCapacity, OrderDataByPrority.

INSTANCE).

There are two ways to do this. Either way, you want to create a custom object that holds both the String (the value you want) and the integer (the priority). The first solution is to have this data object implement Comparable: class Data implements Comparable { private final String message; private final int priority; public Data(String message, int priority) { this.

Message = message; this. Priority = priority; } @Override int compareTo(Data other) { return Integer. ValueOf(priority).

CompareTo(other. Priority); } // also implement equals() and hashCode() } Then when you do PriorityQueue queue = new PriorityQueue(); the queue will order items by the order defined by the compareTo method. The problem with this solution is that if you want the ordering to be only on the integer, then either equals method and your compareTo method will not be consistent or your equals method will not be correct.

A better solution would be to use the PriorityQueue constructor that takes a Comparator. In this case, Data wouldn't have to implement Comparable; you just need a Comparator that defines your ordering: public final class OrderDataByPriority implements Comparator { public static final OrderDataByPriority INSTANCE = new OrderDataByPriority(); private OrderDataByPriority() {} @Override public int compare(Data data1, Data data2) { return Integer. ValueOf(data1.

Priority). CompareTo(data2. Priority); } @Override public boolean equals(Object other) { return other == OrderDataByInteger.

INSTANCE; } private Object readResolve() { return INSTANCE; } } Note that since this comparator takes no data, I made it a singleton. You can then create the queue line this: PriorityQueue queue = new PriorityQueue( initialCapacity, OrderDataByPrority. INSTANCE).

I am using BUfferedReader to read the input from users.. so they put in the message, then hit enter, then put in the integer(priorty)... now when I do queue. Add("string") .. but I need to add them both.. how would I do that? – Shonna Oct 25 '10 at 3:15 I am going to be printing out the strings in descending order.. but its the integer that I will be using to do the comparison... so the integer will need to be connected to the string they entered someway – Shonna Oct 25 '10 at 3:16 You would do queue.

Add(new Data(message, priority)); – NamshubWriter Oct 25 '10 at 3:17 ooh I see.. ohk.. i'm going to go try this. Thanks – Shonna Oct 25 '10 at 3:18 I updated the answer to make it clearer that the integer is a priority and to recommend using Comparator instead of Comparable. Good luck!

– NamshubWriter Oct 25 '10 at 3:23.

How about creating a new class that contains those two fields (int and String) and then implementing Comparable (comparing on the int field). Don't forget to override also hashCode() and equals() (see the Comparable class javadoc for reasoning behind overriding these methods). Is that what you are after?

I would have thought that implementing Comparable would have been sufficient. – Catchwa Oct 25 '10 at 2:49 See the class Javadoc for Comparable for why you want to override equals() and hashCode() – NamshubWriter Oct 25 '10 at 2:57 Updated my answer with reference to JavaDoc – Neeme Praks Oct 25 '10 at 3:31.

If you want to use multiple elements as a key, you can create a class that encapsulates both, and then use that type as the key. Likewise for the values. You should make this custom key class Comparable, override equals(), and override hashCode() for the custom key class that you create.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions