wood burning stoves
The moose likes Java in General and the fly likes Java Hashmap Sorting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java Hashmap Sorting" Watch "Java Hashmap Sorting" New topic

Java Hashmap Sorting

Rajendra Prakash
Ranch Hand

Joined: Sep 10, 2009
Posts: 293
I have a sorting issue with a Hashmap. My constraint is that I MUST use the Hashmap and work with existing code. I do a database query and place the results in a Hashmap. When I iterate thru the Hashmap, it loses the original alphabetical sorting done by the database. So, my problem is that I must sort the results coming out of the Hashmap which is then placed into another class.
Srikanth Nutigattu
Ranch Hand

Joined: Oct 30, 2007
Posts: 114
Hashmap is like a 'Dictionary' it stores the Key/Value pairs, Could you elaborate more on what your are trying to fetch? Keys or values?

If you want to fetch all the keys in the Alphabetical order then you can do something like this:

Set<Object> keySet = yourHashMap.keySet();
TreeSet<Object> treeSet = new TreeSet<Object>(keySet);

treeSet will now hold the keys in a sorted order.
Haven't checked but something on these lines...

Rajendra Prakash
Ranch Hand

Joined: Sep 10, 2009
Posts: 293
Can you give complete code both sorted keys and sorted values
Rajendra Prakash
Ranch Hand

Joined: Sep 10, 2009
Posts: 293
Hi Friend, i have objects stored with hashmap. Now i want to retrieve with sorted order both key as well as value.
shukla raghav
Ranch Hand

Joined: Aug 03, 2008
Posts: 203
There is no problem with your HashMap implementation but the problem is the iterator that you are using. the HashMap class does not have an iterator of its own. it just returns you an object of type Set and we have to obtain our iterator from this Set object. Remember a Set does NOT ensure sorted Data but it does ensure uniqueness of data. so an iterator from the set provides you unsorted unique data. if you wish to sort it you have to pass this Set object to a SortedSet Object. TreeSet is considered to be the Only SortedSet implementation.

So i must say that i have provided you the reason why Srikanth is correct.
Rob Spoor

Joined: Oct 27, 2005
Posts: 20136

Rajendra Prakash wrote:Can you give complete code both sorted keys and sorted values

We are not a code mill.

As for the sorting problem:
1) a HashMap is unsorted. There is nothing you can do about that. A LinkedHashMap can keep the insertion order, but that's as far as you get in regards to the order.
2) A SortedMap (with TreeMap as the most used implementation) is the only option when you want to sort on the keys.
3) Sorting on values is not possible in the current Map implementations. You would need to write one of your own. This topic has been addressed many times before; if you search on this forum you should get multiple results.

If you want alphabetical sorting you can use a TreeMap as both Srikanth Nut and shukla raghav have suggested. If the database provides this same ordering you can also use a LinkedHashMap.

How To Ask Questions How To Answer Questions
Rahul P Kumar
Ranch Hand

Joined: Sep 26, 2009
Posts: 188
In some other topic, Campbell given a useful link http://commons.apache.org/collections/userguide.html#Bidirectional%20Maps. May be you can use that.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 44563
Rahul.p Kumar wrote: . . . Campbell given a useful link . . . May be you can use that.
Probably not for this purpose, I am afraid, but it is still worth looking.
I agree. Here's the link: http://aspose.com/file-tools
subject: Java Hashmap Sorting
It's not a secret anymore!