wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why is the Treemap output like this? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Why is the Treemap output like this?" Watch "Why is the Treemap output like this?" New topic
Author

Why is the Treemap output like this?

Kedar Pethe
Ranch Hand

Joined: Jul 17, 2012
Posts: 39

Output - 1,11,4,9,

If TreeMap sorts in ascending order and String implements Comparable implicitly then why is the output like this??
Shouldn't it be in ascending order - 1,4,9,11
Pritish Chakraborty
Ranch Hand

Joined: Jun 12, 2012
Posts: 91

Strings follow a natural sorting order, remember that this a String we're dealing with, not actual numbers!

Compare the first positions of all the Strings first. If two first positions are the same, compare by the next position and so on. (Kind of how we find words in the dictionary)

1 comes first, so 1 and then 11 (1 before 11 because there is nothing at the second position there).

Then come 4 and 9, as expected.

Also remember that spaces sort before characters, and uppercase characters sort before lowercase ones.

When dealing with only one set of characters, follow the alphabetical order.

Hope this helps


OCJP 6
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

Its just as Pritish said. The hashCode for strings are defined in that way. You can verify it looking at the String's implementation of the hashCode.


[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
Kedar Pethe
Ranch Hand

Joined: Jul 17, 2012
Posts: 39
Pritish Chakraborty wrote:Strings follow a natural sorting order, remember that this a String we're dealing with, not actual numbers!

Compare the first positions of all the Strings first. If two first positions are the same, compare by the next position and so on. (Kind of how we find words in the dictionary)

1 comes first, so 1 and then 11 (1 before 11 because there is nothing at the second position there).

Then come 4 and 9, as expected.

Also remember that spaces sort before characters, and uppercase characters sort before lowercase ones.

When dealing with only one set of characters, follow the alphabetical order.

Hope this helps


Thanks for the reply..
But my query is if you say that we are not dealing with numbers then how does the jvm know that value 4 comes before 9 (as shown in the output) ?

Also please tell me what do you mean by spaces sort before characters..., does the above sorting sequence and spaces sort before characters thing imply sorting according to their ASCII values??
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Kedar Pethe wrote:does the above sorting sequence and spaces sort before characters thing imply sorting according to their ASCII values??


Yes, that's correct. Actually the default sequence for characters follows the Unicode character set, of which ASCII is just the first 128 characters.
Kedar Pethe
Ranch Hand

Joined: Jul 17, 2012
Posts: 39
Paul Clapham wrote:
Kedar Pethe wrote:does the above sorting sequence and spaces sort before characters thing imply sorting according to their ASCII values??


Yes, that's correct. Actually the default sequence for characters follows the Unicode character set, of which ASCII is just the first 128 characters.


Thanks Paul!!
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

Paul Clapham wrote:
Kedar Pethe wrote:does the above sorting sequence and spaces sort before characters thing imply sorting according to their ASCII values??


Yes, that's correct. Actually the default sequence for characters follows the Unicode character set, of which ASCII is just the first 128 characters.


I guess this is the reason i pointed to check the hashCode of the String class.
 
 
subject: Why is the Treemap output like this?
 
Similar Threads
how can be retrieve from any collection into our own order?
TreeMap
Another question from mock exam
Regarding java.lang.Comparator
Hashmap in java