wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Natural Ordering with PriorityQue and String representations of integers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Natural Ordering with PriorityQue and String representations of integers" Watch "Natural Ordering with PriorityQue and String representations of integers" New topic
Author

Natural Ordering with PriorityQue and String representations of integers

Ted North
Ranch Hand

Joined: Jan 02, 2012
Posts: 178
    
    1



You who read me now: I am studying for the SCJP and ran across a problem i can not figure out (yes, this was the only one... lol). The issue has to do with Collections found in java's util class. My problem, in the most general sense, is I do not understand the output...


(Taken from question 9 in the Chapter 7 self-exam)

9. Given the proper import statement(s), and
The code:



The output is: 2 2 3 4

I put the code inside a class with a main method and ran it and obtained the output as shown in the answers section of the great-most-excellent SCJP book by Bates and Sierra (2008).
However, I am COMPLETELY BAFFLED as to how for instance the peek() method on line 16 produces 2 instead of 4? I know that peek returns the highest number and I am not sure how this works with string representations of numbers as opposed to actual primitive types like integer, long, short, double, float, etc.

From the API of PriorityQue on the peek method:peek()
Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty
.

Java API PriorityQue


I have scoured the Internet via Google and found nothing explaining how string representations of integers are ordered. Can some kind intelligent life-form help me understand this?

I want to be ready for the exam

All of my Java,

Ted
Sebanti Sanyal
Ranch Hand

Joined: Nov 07, 2011
Posts: 58

The natural order for numeric types would be ascending order of magnitude(lowest value comes first). For string representations of numbers, it is slightly different.
Example:
Ordering of PriorityQueue<Integer>:1,2,4,33
Ordering of PriorityQueue<String>:1,2,33,4 -->First character '3' in '33' sorts before '4'
Verify the ordering by
Ted North
Ranch Hand

Joined: Jan 02, 2012
Posts: 178
    
    1

A thousand thank yous Sebanti,

Oh man... I can't believe it is the same order basically with integer representations of Strings as it is for integers. Thank you for supplying the code, that helped me understand. I obviously did not grasp how each method actually behaves. I was surprised it printed the entire PriorityQue instead of printing and removing the head of the que.

I could not brain yesterday, I had the dumb.

May the Java be with You,

Ted North
Larsen Raja
Ranch Hand

Joined: Nov 28, 2011
Posts: 58
1. peek doesnt return the highest element but the element with highest priority.
2. PriorityQueue follows First In First Out order.
3. With your example, priority is based on natural ordering.

when you add 2,4,1,3 into the queue with a priority of natural ordering, it gets ensembled as <1,2,3,4> and not <2,4,1,3>.
offer and add does the same thing with respect to adding to a priorityqueue.

Ted North
Ranch Hand

Joined: Jan 02, 2012
Posts: 178
    
    1

Hi Larsen Raja,

Thank you for explaining this further. I had to study Sebanti Sanyal's response to understand that peek in this case returns the first element and not the largest number. Thank you again for driving this point home, as the expression goes.

Java-the-planet,

Ted North


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Natural Ordering with PriorityQue and String representations of integers
 
Similar Threads
Priority Queue
Query related to Priority queue
PriorityQueue()
PriorityQueue Ordering
Print contents of PriorityQueue not working