| Author |
sorting the values using Hsh Map
|
Raja Sekhar
Greenhorn
Joined: Oct 10, 2007
Posts: 8
|
|
Hi All, I need to sort the names and corresponding the values are sorted to it and need to be displayed , can anyone suggest the required method.
|
 |
satyabrat mishra
Greenhorn
Joined: Oct 24, 2007
Posts: 3
|
|
Map<String, object> set = new TreeMap<String, object>(); put names as key and values as value. it will automatically sort the names and values. [ October 29, 2007: Message edited by: satyabrat mishra ]
|
certification is not the proof of knowledge.......
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12950
|
|
A HashMap is not a sorted collection, so you cannot sort items while they are in a HashMap. A TreeMap sorts its elements by key.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
|
|
|
If you need to retrieve values by key and iterate them in sequence, you may have to use two collections. You can get the values as a set and make a TreeSet. That will not only sort them, but will also remove duplicates, which may or may not be a good thing.
|
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
|
 |
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
|
|
To be clear, Jesper is talking about sorting by the key, and Stan is apparently talking about sorting by value. It's not immediately clear which is desired here, although I think sorting by key is more common. If you want to sort values without eliminating duplicates, don't put them in a Set - just use toArray() and then use Arrays.sort(). [ October 29, 2007: Message edited by: Jim Yingst ]
|
"I'm not back." - Bill Harding, Twister
|
 |
 |
|
|
subject: sorting the values using Hsh Map
|
|
|