File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
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
I agree. Here's the link:
subject: HashMap sorting
It's not a secret anymore!