Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Collection  RSS feed

 
vidya gopal
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In TreeMap, I would like to sort the Treemap not as per the default ordering- but based on the values. Any body has any clue?
 
Dmitry Melnik
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it's quite doable. I just got a clue by reading class documentation at http://java.sun.com/j2se/1.4.2/docs/api/java/util/TreeMap.html

I would implement a Comparator and pass it to TreeMap constructor.
 
vidya gopal
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply.

But in the comparator definition, how would i access the map entries. Should i have to case them as Map.Entry - or I would I access the map value for comparison?
 
Dmitry Melnik
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I misunderstood you, I apologize.

Since values of the objects which you plan to keep in a sorted map contain all the key data, why not to keep them in a sorted set (like TreeSet) with a custom Comparator?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!