In Java, the PriorityQueue
class is implemented as a priority heap. Heap is an important data structure in computer science. For a quick overview of heap, here is a very good tutorial.
1. Simple Example
The following examples shows the basic operations of PriorityQueue such as offer(), peek(), poll(), and size().
import java.util.Comparator; import java.util.PriorityQueue; public class PriorityQueueTest { static class PQsort implements Comparator<Integer> { public int compare(Integer one, Integer two) { return two - one; } } public static void main(String[] args) { int[] ia = { 1, 10, 5, 3, 4, 7, 6, 9, 8 }; PriorityQueue<Integer> pq1 = new PriorityQueue<Integer>(); // use offer() method to add elements to the PriorityQueue pq1 for (int x : ia) { pq1.offer(x); } System.out.println("pq1: " + pq1); PQsort pqs = new PQsort(); PriorityQueue<Integer> pq2 = new PriorityQueue<Integer>(10, pqs); // In this particular case, we can simply use Collections.reverseOrder() // instead of self-defined comparator for (int x : ia) { pq2.offer(x); } System.out.println("pq2: " + pq2); // print size System.out.println("size: " + pq2.size()); // return highest priority element in the queue without removing it System.out.println("peek: " + pq2.peek()); // print size System.out.println("size: " + pq2.size()); // return highest priority element and removes it from the queue System.out.println("poll: " + pq2.poll()); // print size System.out.println("size: " + pq2.size()); System.out.print("pq2: " + pq2); } } |
Output:
pq1: [1, 3, 5, 8, 4, 7, 6, 10, 9] pq2: [10, 9, 7, 8, 3, 5, 6, 1, 4] size: 9 peek: 10 size: 9 poll: 10 size: 8 pq2: [9, 8, 7, 4, 3, 5, 6, 1]
2. Example of Solving Problems Using PriorityQueue
For more details about PriorityQueue, please go to doc.
done all
Which of the following declaration is valid in the Java Collections Framework?
Select one:
HashMap map = new Map();
Collection coll = new HashMap();
HashSet list = new Set();
Collection coll = new HashSet();
Which of the following exceptions is unchecked?
Select one:
IllegalAccessException
ClassNotFoundException
NoSuchMethodException
ArithmeticException
If the class Mammal is declared as an class, with Dog, Cat, and Horse as classes that extend Mammal, which of the following declarations is valid?
Select one:
Wombat myWombat;
Dog myDog = new Mammal();
Dog myDogCat = new Cat();
Mammal myDog = new Mammal();
If a Priority Queue was defined as PriorityQueue pq and had the sequence of numbers 10, 3, 6, 8, 9 added to it. What would be the output of the following code
pq.poll();
int temp = pq.poll();
System.out.println(temp);
Select one:
9
6
8
5
3
If you require a collection class that can store objects in a sequence and has fast random element access an informed choice for a class would be:
Select one:
Collection
HashSet
LinkedList
TreeSet
ArrayList
Given the following calls to the variable Stack s , what will be the output.
s.push(5);
s.push(7);
s.push(9);
s.pop();
System.out.println(s.pop());
Select one:
5
9
7
[5]
————-
Given the following definition of a compare method:
public int compare(String o1, String o2) {
int val = o1.length() – o2.length();
return val;
}
what would be the sort order of the following objects in variable list after calling Collections.sort(list)?
ArrayList list = new ArrayList();
list.add(“aaa”); list.add(“b”); list.add(“cc”);
Select one:
[cc, bb, aaa]
[aaa, b, cc]
[aaa, cc, b]
[b, cc, aaa]
————
What would be the equivalent for loop expression for the follow while loop?
int i = 1;
while (i <= 10)
{
// use the value of i
…
i = i + 1;
}
Select one:
for (int i = 0; i = 0; i–)
for (int i = 1; i 0; i++)
If getter and setters are req, then we have to
Do we need to use getter and setter methods to make program look impressive?
Another link:
http://writeulearn.com/virtual-destructor-c/
It is a binary heap inside queue.
should the result be either ascending or descending?
[1, 3, 5, 8, 4, 7, 6, 10, 9] -> [1, 3, 4, 5, 6, 7, 8, 9, 10]
In compare method what if variable one or two has negative value? In such cases this compare method wont result correctly. Instead it should be:
public int compare(Integer one, Integer two) {
return two.compareTo(one);
}
It always recommends to use compareTo method on every object while comparing.
“Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.”
(second-first) is equivalent to -(first-second). Consider the following node(ascending order), see if it helps.
if (first == second)
return 0;
else if (first > second)
return 1;
else
return -1;
public int compare(Integer one, Integer two) {
return two – one;
}
how this is reversing??what is the logic behind?
another link on tech forum: http://www.writeulearn.com/priority-queue/
From Java Doc –
Comparators can be passed to a sort method (such as Collections.sort or Arrays.sort) to allow precise control over the sort order. Comparators can also be used to control the order of certain data structures (such as sorted sets or sorted maps), or to provide an ordering for collections of objects that don’t have a natural ordering.
Why do we use comparator?