wood burning stoves 2.0*
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

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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: 443


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: 410

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);
subject: Sort a Map on the <Key>.
Similar Threads
collections query
Code to sort and remove duplicates in ArrayList and Hashmap
Why is this called Meaningless Drivel?
Simplifying list of "time" options with jsp
Sorting values of a Map.