Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HashMap sorting

 
thippareddy mallikarjuna reddy
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,

This is mallikarjuna Reddy. My HashMap having key based values. some times values may be duplicate. so need to sort HashMap in the desc order. please help me in this problem.

Thanks
Mallikarjuna
 
ak pillai
author
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should use a TreeMap for sorting purpose. By default it is sorted by natural-order (i.e. using the compareTo method). You could also add a Comparator to have a better control over your sorting. Checkout the TreeMap API. Example:




You could also pass your existing HashMap as an argument.




Also experiment with Comparators.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic