java sort map by values

Solutions on MaxInterview for java sort map by values by the best coders in the world

showing results for - "java sort map by values"
Jonah
26 Nov 2017
1Map<String, Integer> unSortedMap = getUnSortedMap();
2         
3System.out.println("Unsorted Map : " + unSortedMap);
4 
5//LinkedHashMap preserve the ordering of elements in which they are inserted
6LinkedHashMap<String, Integer> sortedMap = new LinkedHashMap<>();
7 
8unSortedMap.entrySet()
9    .stream()
10    .sorted(Map.Entry.comparingByValue())
11    .forEachOrdered(x -> sortedMap.put(x.getKey(), x.getValue()));
12 
13System.out.println("Sorted Map   : " + sortedMap);
14 
15Output:
16 
17Unsorted Map : {alex=1, charles=4, david=2, brian=5, elle=3}
18Sorted Map   : {alex=1, david=2, elle=3, charles=4, brian=5}
19
Edoardo
28 Feb 2020
1//----------------------------------------------------------------------
2// in ascending order:
3Map<String, Integer> unSortedMap = getUnSortedMap();
4         
5System.out.println("Unsorted Map : " + unSortedMap);
6 
7//LinkedHashMap preserve the ordering of elements in which they are inserted
8LinkedHashMap<String, Integer> sortedMap = new LinkedHashMap<>();
9 
10unSortedMap.entrySet()
11    .stream()
12    .sorted(Map.Entry.comparingByValue())
13    .forEachOrdered(x -> sortedMap.put(x.getKey(), x.getValue()));
14 
15System.out.println("Sorted Map   : " + sortedMap);
16 
17Output:
18 
19Unsorted Map : {alex=1, charles=4, david=2, brian=5, elle=3}
20Sorted Map   : {alex=1, david=2, elle=3, charles=4, brian=5}
21
22
23//----------------------------------------------------------------------
24// in descending order:
25Map<String, Integer> unSortedMap = getUnSortedMap();
26         
27System.out.println("Unsorted Map : " + unSortedMap);
28 
29//LinkedHashMap preserve the ordering of elements in which they are inserted
30LinkedHashMap<String, Integer> reverseSortedMap = new LinkedHashMap<>();
31 
32//Use Comparator.reverseOrder() for reverse ordering
33unSortedMap.entrySet()
34    .stream()
35    .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())) 
36    .forEachOrdered(x -> reverseSortedMap.put(x.getKey(), x.getValue()));
37 
38System.out.println("Reverse Sorted Map   : " + reverseSortedMap);
39 
40Output:
41 
42Unsorted Map        : {alex=1, charles=4, david=2, brian=5, elle=3}
43Reverse Sorted Map  : {brian=5, charles=4, elle=3, david=2, alex=1}
44
queries leading to this page
sort map by value in java 8java sort treemap by valuemap sort by keys and value javajava asc ordering a map according to valuesort map by valuesjava map create map to order by valuejava map comparator by valuejava sorting map by keyjava sort dictionary by value functionjava sort map by comparatorjava 8 filter value ascendingsort map based on value java easy java sort map by long valuessort map based on value javahow to sort the map by value in javajava 8 treemap sort by valuehow to sort array in descending order in c 2b 2bhow to sort through the values in map in javajava sort map by valueorder by map on values javajava ordering a map according to valuemap sort 28 29sort map based on value java 8sorting java map by valuejava treemap sorted by valuesorting a map by values jvaajava created map sort by valuesort values in map javaorder values from map javahow to sort map by values in javaorder map by value javasorting map values in javasort map by value in java 7collections sort map by valuejava sort map with array as valuemap compare by value reverse javajava sort map by value streamssort map by key javajava treemap sort by valuesort treemap by value in descending order javajava sort map by value variablejava sorted map on valuejava sort list by valuessort map according to value javahow to sort a map based on valuesjava map with sorted valuesjava map with comparator by valuesort a map with their value javasort map in map javahow to sort through map and return highet average in java 8sort map by values in javamap in java sorted acc to valuessort map on the basis of value java 8sort map values by field javasorting map by value in javasort a map by value java8java map value as map sortedtreemap sort descending javaorderby map on values java map sorted by value javajava map get sorted valuessorting map by value javajava sorting map by valuesort map javacan treemap sort on value in javatreemap sort by value descendingjava map c2 a8sort by valuejava map sort on valuesort by value map 2b java8java order map by valuehow to sort a treemap by value in javajava sort a map by valuehow to sort a map w r t values in javasort map by value then by key java 8how to sort treemap by value in javasort a map and return a map in java java stream order map by valuesort values integer in hash map javajava sort treemap by valuessort treemap by value in javamap sort by value javasorting a map in java by valuejava sort map by values listjava sort dictionary by value variable map sortsort map by value javaqmap sort by value in javasorting an hashmap based on values in javamap of map sort by value stream javasort and mapjava map sorted by valuejava sort dictionary by valuemap order by value javahow to sort a dict by values javajava treemap sort by value in reversesort map by values in java 8map java order by valuejava map that sorts by valuesort the map by value in javasort the map by valuessort map value in javasort a map by values in an arrayhow to sort a map by value in javaorder map by values javamap sort by valuejava sort map with arraysort the map by value in reverse order in javasort map java keysjava treemap sort by value descendinghow to sort map with valuestreemap sort on valuesort map by value java 8sort map by key value javasort map based on values javahow to sort map by value in javasort items in a map javamap order by value java 8java map sort by value descendinghow to sort values in a map javastore map order by value javasort map using values javasort map by value in javamap sort by value java 8how to sort a map by valuesjava map sort by valuejava map order by valuesort items in a maphow to sort treemap in descending order in javamap java order by valuesort map by value in java without using collectionsjava map sort by value then keyhow to sort map by value in java 7how to sort map by value in java 8map of map sort by value javahasmmap sort by higest value javajava8 sort map with valuessort map entries by value javasort map values based in javasort a dictionary by value javasort a map by value javajava map values ordersorting map wrt values in javasorted map by value javasort value by map javasort map by valuehashmap sort based on values javajava 8 sort map by valuesort map in javajava sort hashmap by valuesjava map sorting by valuehow to sort map by values in jvasort map values in java 8map sort by value greatest to least javasort map with value javajava util map sorthashmap orderby value lowestsorting map by value in java 8order a map by value javamap sortsort map entry javajava map sort based on valuesort map by value stream javajava sort map based on valuejava entryset sort by valuesort treemap in descending order by valuesort map in java by valuehow to sort a map by valuesort the values of a map in javasort map by values javajava ordering a mapsorting a map by value in javahow to sort map in java using valueshow to sort map according to values javajava sort map by valuesmap sorted by value javakeep map sorted by values javasort map by values java 8java how to sort map by valuejava sort map by key and valuesorting a map by value javajava sort map on valueshow to sort map by valuejava maps with sorting by valuesort map by value in java explainedsort a map by value java 8sort by value in javahow to sort mapsorting hashmap by values in javajava how to sort a map by valuesjava entry set sort by valuesort treemap by value descending javasort map based on value in java 7how to sort a map in java by valuesort java map by valuesort map by value javasort elements in map javasort map by key and value javacomparator to sort map in javahow to sort elements in map in javatreemap sort by value javajava sort maphow to sort a map based on values in javajava sort map by values