Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!

# poll(),peek() in PriorityQueue class

jeya prabha
Greenhorn
Posts: 23
Anybody know about these two methods .
for example
public class Test {

/** Creates a new instance of Test */
public static void main(String args[]) {
Queue<String> queue=new PriorityQueue<String>();
queue.add("one");
queue.add("two");
queue.add("three");
queue.add("four");
queue.add("five");
queue.add("FIVE");
queue.add("Four");
System.out.println(queue.poll());
System.out.println(queue.peek());
System.out.println(queue.size());
}

}

output is
one
three
2
please explain how this methods are working.
thanks.

Brij Garg
Ranch Hand
Posts: 234
This can not be the correct output.

Ove LindstrÃ¶m
Ranch Hand
Posts: 326
• 1
The output would be
FIVE
four
6

regardless of the order that you add the Strings.

What the PriorityQueue does is to order the objects according to their natural order when added.

So if you do this code (R# is the row number, [...] is the queue):

it will do this.
R1: Nice, I got a string 'one' and put that in the empty cue [one].
R2: Ah, a string 'Four'. That one goes before the string 'one'. [Four,one]
R3: A string 'two'. That goes into the queue after 'one'. [Four,one,two]
...
R7: A string 'three'. Lets sort that one into the queue. [FIVE, five, Four, one, four, two, three].
R9: Let me get the first object (poll it) out of the queue. That is the String 'FIVE'. [Four, five, three, one, four, two]
R10: Now, lets just peek at the next object in the queue, but do not remove it from the queue. That object is the string 'Four'. [Four, five, three, one, four, two]
R11: Let us count the objects in the queue.

jeya prabha
Greenhorn
Posts: 23
i can not understand this.
how it comes ?
what it means natural order?
is it means alphabetical order?
please help me out

VidyaVikas Yalamanchili
Greenhorn
Posts: 10
HI,

The Natural Order is Alphabetical Order only.For Strings Capitals are first after that Small Letters thats why in yiur programmes FIVE got polled first and it got removoed the next element is Four not five because F is capita Letter.So it gave Four as next output but peek wont take the element out.So the length is 6.

I think it explains why your output is FIVE Four 6

Originally posted by jeya prabha:
i can not understand this.
how it comes ?
what it means natural order?
is it means alphabetical order?
please help me out

[ September 03, 2008: Message edited by: VidyaVikas Yalamanchili ]

jeya prabha
Greenhorn
Posts: 23
HI ,
Now i can understand this.
for my understanding
: queue.add("one");
R2: queue.add("Four");
R3: queue.add("two");
R4: queue.add("four");
R5: queue.add("five");
R6: queue.add("FIVE");
R7: queue.add("three");
R8:

R10:System.out.println(queue.peek());
R11:System.out.println(queue.size());

if i am directly peek this means
the output is FIVE .
is it correct?
please help me out.
thanks.

VidyaVikas Yalamanchili
Greenhorn
Posts: 10
Originally posted by jeya prabha:
HI ,
Now i can understand this.
for my understanding
: queue.add("one");
R2: queue.add("Four");
R3: queue.add("two");
R4: queue.add("four");
R5: queue.add("five");
R6: queue.add("FIVE");
R7: queue.add("three");
R8:

R10:System.out.println(queue.peek());
R11:System.out.println(queue.size());

if i am directly peek this means
the output is FIVE .
is it correct?
please help me out.
thanks.

Hi yes thats true.If you are not adding FIVE and add Five then peek will be Five

Campbell Ritchie
Sheriff
Posts: 48445
56
Originally posted by jeya prabha:
Anybody know about these two methods .
for example
public class Test {

/** Creates a new instance of Test */
public static void main(String args[]) {
Queue<String> queue=new PriorityQueue<String>();
queue.add("one");
queue.add("two");
queue.add("three");
queue.add("four");
queue.add("five");
queue.add("FIVE");
queue.add("Four");
System.out.println(queue.poll());
System.out.println(queue.peek());
System.out.println(queue.size());
}

}

output is
one
three
2
please explain how this methods are working.
thanks.
Have you actually run that method? I compiled it with cut-and-paste from your posting and got this output:
java PriorityQueueTest
FIVE
Four
6
[3]+ Done
How on earth did you get "one three 2"?

vinod ernakulam
Greenhorn
Posts: 7
Can any body please explain why "one" is coming before "four" and "two" is coming before "three" ?

Thanks.

Rob Spoor
Sheriff
Posts: 20495
54
vinod vp wrote:Can any body please explain why "one" is coming before "four" and "two" is coming before "three" ?

Thanks.

Unless you use a custom comparator, "four" comes before "one" and "three" comes before "two" just as you expected. With a custom comparator, you can do just about anything.