File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Hashmap Sorting

 
Rajendra Prakash
Ranch Hand
Posts: 293
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 114
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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...

HTH
 
Rajendra Prakash
Ranch Hand
Posts: 293
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you give complete code both sorted keys and sorted values
 
Rajendra Prakash
Ranch Hand
Posts: 293
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 203
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rahul P Kumar
Ranch Hand
Posts: 188
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 47244
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic