sorting - Sort a Map<Key, Value> by values (Java) -
i relatively new java, , find need sort map<key, value>
on values. since values not unique, find myself converting keyset
array
, , sorting array through array sort custom comparator sorts on value associated key. there easier way?
here's generic-friendly version you're free use:
import java.util.*; public class maputil { public static <k, v extends comparable<? super v>> map<k, v> sortbyvalue(map<k, v> map) { list<map.entry<k, v>> list = new linkedlist<map.entry<k, v>>(map.entryset()); collections.sort( list, new comparator<map.entry<k, v>>() { public int compare(map.entry<k, v> o1, map.entry<k, v> o2) { return (o1.getvalue()).compareto( o2.getvalue() ); } }); map<k, v> result = new linkedhashmap<k, v>(); (map.entry<k, v> entry : list) { result.put(entry.getkey(), entry.getvalue()); } return result; } }
and associated junit4 test don't have take word it:
import java.util.*; import org.junit.*; public class maputiltest { @test public void testsortbyvalue() { random random = new random(system.currenttimemillis()); map<string, integer> testmap = new hashmap<string, integer>(1000); for(int = 0; < 1000; ++i) { testmap.put( "somestring" + random.nextint(), random.nextint()); } testmap = maputil.sortbyvalue(testmap); assert.assertequals(1000, testmap.size()); integer previous = null; for(map.entry<string, integer> entry : testmap.entryset()) { assert.assertnotnull(entry.getvalue()); if (previous != null) { assert.asserttrue(entry.getvalue() >= previous); } previous = entry.getvalue(); } } }
java 7 version
public static <k, v extends comparable<? super v>> map<k, v> sortbyvalue(map<k, v> map) { list<map.entry<k, v>> list = new linkedlist<>(map.entryset()); collections.sort( list, new comparator<map.entry<k, v>>() { @override public int compare(map.entry<k, v> o1, map.entry<k, v> o2) { return (o1.getvalue()).compareto(o2.getvalue()); } }); map<k, v> result = new linkedhashmap<>(); (map.entry<k, v> entry : list) { result.put(entry.getkey(), entry.getvalue()); } return result; }
java 8 version. sort according value in ascending order; descending order, possible uncomment call collections.reverseorder()
.
public static <k, v extends comparable<? super v>> map<k, v> sortbyvalue(map<k, v> map) { return map.entryset() .stream() .sorted(map.entry.comparingbyvalue(/*collections.reverseorder()*/)) .collect(collectors.tomap( map.entry::getkey, map.entry::getvalue, (e1, e2) -> e1, linkedhashmap::new )); }
there 1 more technique sort hashmap values. here, no comparator used. sort based on keys, swap keys , values, sort based on values , again swap finalmap, sorted hashmap based on values.
private static linkedhashmap<string, string> method1(hashmap<string, string> originalhashmap) { linkedhashmap<string, string> sortedhashmapbykeys = new linkedhashmap<>(); //maintains order of putting treemap<string, string> originaltreemap = new treemap<>(originalhashmap); //sorts based on keys (map.entry<string, string> map: originaltreemap.entryset()) { sortedhashmapbykeys.put(map.getkey(), map.getvalue()); } linkedhashmap<string, string> reversedofsortedlinkedhashmap = new linkedhashmap<>(); (map.entry<string, string> map: sortedhashmapbykeys.entryset()) { reversedofsortedlinkedhashmap.put(map.getvalue(), map.getkey()); } linkedhashmap<string, string> finalmap = new linkedhashmap<>(); treemap<string, string> treemapofreversedofsortedlinkedhashmap = new treemap<>(reversedofsortedlinkedhashmap); (map.entry<string, string> map: treemapofreversedofsortedlinkedhashmap.entryset()) { finalmap.put(map.getkey(), map.getvalue()); //sort , swap } return finalmap; }
Comments
Post a Comment