This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why is the Treemap output like this?

 
Kedar Pethe
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 91
C++ Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kedar Pethe
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20948
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic