Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Collections..

sweety singh
Ranch Hand
Posts: 49
import java.util.*;

public class PQ {

static class PQsort implements Comparator<Integer>
{
public int compare(Integer one, Integer two)
{
return two.compareTo(one);

}
}

public static void main(String[] args)
{
int [] ia = {1, 5 ,3 , 7 ,6, 9 ,8,};
PriorityQueue<Integer> pq1 = new PriorityQueue<Integer>() ;
for (int x : ia)
pq1.offer(x);
for (int x : ia)
System.out.print(pq1.poll() + " ");

System.out.println(" ");

PQsort pqs = new PQsort();
PriorityQueue <Integer> pq2 = new PriorityQueue<Integer>(10,pqs);

for (int x : ia)
pq2.offer(x);
System.out.println("size " + pq2.size());
System.out.println("peek " + pq2.peek());
System.out.println("size " + pq2.size());
System.out.println("poll " + pq2.poll());
System.out.println("size " + pq2.size());

for (int x : ia)
System.out.println(pq2.poll() + " ");
}

}

// i have not understood this program can somebody expalin it to me line by line... thanks..
[ May 23, 2008: Message edited by: sweety singh ]

sherin stephen
Greenhorn
Posts: 6
this is a sample code for PriorityQueue implementation.
"An unbounded priority queue based on a priority heap. 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. A priority queue does not permit null elements. A priority queue relying on natural ordering also does not permit insertion of non-comparable objects (doing so may result in ClassCastException)"

pq.offer(E e) inserts the element
pq.peek() gives the last inserted element
pq.poll() takes out the last inserted element
pq.size() gives you the number of elements in the queue.

as simple as that ....

the program initially puts the array elements into the queue (in the natural order)and the prints it

the next constructor gives in a comparator to define the ordering (reverse in this case).

Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15568
46
"zzyborg", please check your private messages. You can see them by clicking My Private Messages.

Gary Kevin
Ranch Hand
Posts: 43
which part of the program you can't understand?inner class or Generics or methods of class PriorityQueue ?
[ May 23, 2008: Message edited by: Ulf Dittmer ]

sweety singh
Ranch Hand
Posts: 49
Originally posted by sweety singh:
import java.util.*;

public class PQ {

static class PQsort implements Comparator<Integer>
{
public int compare(Integer one, Integer two)
{
return two.compareTo(one);

}
}

public static void main(String[] args)
{
int [] ia = {1, 5 ,3 , 7 ,6, 9 ,8,};
PriorityQueue<Integer> pq1 = new PriorityQueue<Integer>() ;
for (int x : ia)
pq1.offer(x);
for (int x : ia)
System.out.print(pq1.poll() + " ");// 1

System.out.println(" ");

PQsort pqs = new PQsort();
PriorityQueue <Integer> pq2 = new PriorityQueue<Integer>(10,pqs);

for (int x : ia)
pq2.offer(x);
System.out.println("size " + pq2.size());
System.out.println("peek " + pq2.peek());
System.out.println("size " + pq2.size());
System.out.println("poll " + pq2.poll());
System.out.println("size " + pq2.size());

for (int x : ia)
System.out.println(pq2.poll() + " ");
}

}

// i have not understood this program can somebody expalin it to me line by line... thanks..

what is happening in line 1... poll() will remove the priority element.. but it displays all...???..
[ May 23, 2008: Message edited by: sweety singh ]

sweety singh
Ranch Hand
Posts: 49
i still havent understood the same line...

Ulf Dittmer
Rancher
Posts: 42968
73
The poll() call happens in a loop, so this line successively removes all elements, and prints each one as it does so.

Gary Kevin
Ranch Hand
Posts: 43
Originally posted by sweety singh:
i still havent understood the same line...

This is the API of poll method.

when you invoke it like this: pq1.poll(),it first "Retrieves and removes the head of this queue",so "poll() will remove the priority element"

second,it " Returns: