Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Natural Ordering with PriorityQue and String representations of integers

 
Ted North
Ranch Hand
Posts: 201
1
Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 58
C++ Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 201
1
Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 58
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 201
1
Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic