It's not a secret anymore!
The moose likes Java in General and the fly likes Sort a Map on the <Key>. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Sort a Map on the <Key>." Watch "Sort a Map on the <Key>." New topic

Sort a Map on the <Key>.

Rajkamal Pillai
Ranch Hand

Joined: Mar 02, 2005
Posts: 445


How do we sort a <Map> on the key? I am using a java.util.HashMap.

pawan chopra
Ranch Hand

Joined: Jan 23, 2008
Posts: 415

I think you can use TreeMap

Pawan Chopra
vikram Kesav
Ranch Hand

Joined: Jul 11, 2008
Posts: 69
Dump the HashMap into a TreeMap:

Map hashMap= new HashMap();
Map sortedMap = new TreeMap(hashMap);
I agree. Here's the link:
subject: Sort a Map on the <Key>.
It's not a secret anymore!