my dog learned polymorphism*
The moose likes Java in General and the fly likes HashMap sorting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "HashMap sorting " Watch "HashMap sorting " New topic

HashMap sorting

thippareddy mallikarjuna reddy

Joined: Mar 08, 2006
Posts: 19
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.

ak pillai
Ranch Hand

Joined: Feb 11, 2006
Posts: 288
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.

java j2ee job interview questions with answers | Learn the core concepts and the key areas
subject: HashMap sorting
Similar Threads
First Key of HashMap
Tomcat Shuts down Automatically after 24hrs
Collection issue
switch on String
order of Map