I'm trying to sort a HashMap by values. I know that it might not be "good style", but it would save a lot of work, as I wouldn't have to rewrite lots of code if I could just get the HashMap sorted by value.
After lots of searching and trial and error cases I'm still stuck.
The HashMap itself is still unsorted.
import java.util.* ; public class H { static HashMap first = new HashMap(); static { first.put("20030120" , new Integer (56)); first.put("20030118" , new Integer (19)); first.put("20030125" , new Integer (25)); first.put("20030122" , new Integer (32)); first.put("20030117" , new Integer (67)); first.put("20030123" , new Integer (34)); first.put("20030124" , new Integer (42)); first.put("20030121" , new Integer (19)); first.put("20030119" , new Integer (98)); } public static void main( String[] args ) { ArrayList as = new ArrayList( first.entrySet() ); Collections.sort( as , new Comparator() { public int compare( Object o1 , Object o2 ) { Map.Entry e1 = (Map.Entry)o1 ; Map.Entry e2 = (Map.Entry)o2 ; Integer first = (Integer)e1.getValue(); Integer second = (Integer)e2.getValue(); return first.compareTo( second ); } }); Iterator i = as.iterator(); while ( i.hasNext() ) { System.out.println( (Map.Entry)i.next() ); } } }
Now my qustion is, how to put those sorted Entry sets back in a HashMap?
This is probably very easy, but I'm just not seeing it.
Thanks in advance for reading. Any input is appreciated.
1 year 43 weeks ago
Give up. HashMap won't store your data sorted in any order, let alone by value.
TreeMap will sort data inserted in arbitrary order, but only by the key. You can write a comparator that tries to sort by values given the key, but it's a mess and won't always work right.
LinkedHashMap, on the other hand, will store items in insertion order. So if you sort by value, as you've shown here, then insert into a LinkedHashMap in sorted order, they'll stay that way.
LinkedHash is the best way to go about what you want, second option could be a custom container with a comparater built in to contain you sorting criteria.
Something like this, maybe?
Regards,
Artem D. Yegorov
Artem, that works if all the values are unique, which isn't true in my case. I ened up with the below. Maybe somebody can make it better, but it does deal with duplicate values.