How does Priority Queue work. This is my scenario, I have certain objects with priorities 1,2,..10. There could be multiple objects with same priority. I was thinking I can put them into a priority queue and let the pool() give me the objects with highest priority.. ?
I m kinda confused with PQ.
1) I get errors when i create a PQ <MyClass>.
2) Do i need to use comparator to ensure i check the priority of the object?

Pointers plz

Thanks
Pushkala

Recommended Answers

All 4 Replies

From the java doc.

>How does Priority Queue work.

The elements of the priority queue are ordered according to their natural ordering, or by a Comparator provided at queue construction time, depending on which constructor is used.

>I get errors when i create a PQ <MyClass>.

A priority queue relying on natural ordering also does not permit insertion of non-comparable objects.

Agreed. I inserted elements in PQ..
But things get weird.
1) Create a class called msg with variable "p"
2) I created PQ and Comparator to handle inserts of instances of msg and compare value of "p"
3) Now i insert more than one msg with same "p" (the various p values are 2,1,3,2)
4) For some reason, after i do all the "offers", when i print the pq,
the order i get is 1,2,3,2
5) Now if i do a peek() or pool(), then, the pq gets ordered and i get 1,2,2,3.
Any pointers..?

From the java doc.

>How does Priority Queue work.

The elements of the priority queue are ordered according to their natural ordering, or by a Comparator provided at queue construction time, depending on which constructor is used.

>I get errors when i create a PQ <MyClass>.

A priority queue relying on natural ordering also does not permit insertion of non-comparable objects.

public class Prioritizer implements Comparator<msgclass>
{

    public int compare(msgclass o1, msgclass o2) {
      int n = o2.getPriority()-o2.getPriority();
       return n;
    }
***************************************
class PQ {
public static void main(String[] args) {
    msgclass a5 = new msgclass();
    a5.setPriority(100);
      msgclass a1 = new msgclass();
    a1.setPriority(10);
      msgclass a2 = new msgclass();
    a2.setPriority(20);
      msgclass a3 = new msgclass();
    a3.setPriority(30);
     msgclass a4 = new msgclass();
    a4.setPriority(30);
     msgclass a6 = new msgclass();
    a6.setPriority(130);

Prioritizer pqs = new Prioritizer(); // get a Comparator
PriorityQueue<msgclass> pq2 =
new PriorityQueue<msgclass>(10,pqs); // use Comparator
pq2.add(a5);
pq2.add(a1);
pq2.add(a2);
pq2.add(a3);
pq2.add(a4);
pq2.add(a6);
System.out.println(pq2);
System.out.println("size " + pq2.size());
System.out.println("PEEK " + pq2.peek());
System.out.println("After PEEK"+pq2);
System.out.println("POOL " + pq2.poll());
System.out.println("After POLL"+pq2);
System.out.println("After POLL"+pq2);
}
}

[(100), (10), (20), (30), (30), (130)]
size 6
PEEK (100)
After PEEK[(100), (10), (20), (30), (30), (130)]
POOL (100)
After POLL[(130), (10), (20), (30), (30)]
After POLL[(130), (10), (20), (30), (30)]

This is the output i get..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.