wood burning stoves 2.0*
The moose likes Java in General and the fly likes poll(),peek() in PriorityQueue class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "poll(),peek() in PriorityQueue class" Watch "poll(),peek() in PriorityQueue class" New topic
Author

poll(),peek() in PriorityQueue class

jeya prabha
Greenhorn

Joined: Jun 03, 2008
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

Joined: Apr 29, 2008
Posts: 234
This can not be the correct output.
Ove Lindström
Ranch Hand

Joined: Mar 10, 2008
Posts: 326

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

Joined: Jun 03, 2008
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

Joined: Nov 26, 2007
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 ]

with Regards,<br />VidyaVikas Yalamanchili SCJP 5.0
jeya prabha
Greenhorn

Joined: Jun 03, 2008
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

Joined: Nov 26, 2007
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

Joined: Oct 13, 2005
Posts: 37897
    
  22
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

Joined: Jun 29, 2009
Posts: 7
Can any body please explain why "one" is coming before "four" and "two" is coming before "three" ?

Thanks.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: poll(),peek() in PriorityQueue class
 
Similar Threads
Mystic with PriorityQueue
PriorityQueue
Priority Queue Ordering
Print contents of PriorityQueue not working
PriorityQueue Problem