sorting hashmap by values in java

Solutions on MaxInterview for sorting hashmap by values in java by the best coders in the world

showing results for - "sorting hashmap by values in java"
Marco
20 May 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
Kevin
23 Jun 2017
1import java.util.Collections;
2import java.util.Comparator;
3import java.util.HashMap;
4import java.util.LinkedHashMap;
5import java.util.LinkedList;
6import java.util.List;
7import java.util.Map;
8public class SortHashMapByValue
9{
10   public static void main(String[] args)
11   {
12      HashMap<String, Integer> hash = new HashMap<String, Integer>();
13      hash.put("Toyota", 78);
14      hash.put("Skoda", 69);
15      hash.put("Honda", 93);
16      hash.put("Audi", 59);
17      hash.put("Chevrolet", 39);
18      hash.put("Hyundai", 56);
19      Map<String, Integer> map = sortByValue(hash);
20      System.out.println("Sorting hashmap by values in java: ");
21      // printing sorted HashMap
22      for(Map.Entry<String, Integer> me : map.entrySet())
23      {
24         System.out.println("Key = " + me.getKey() + ", Value = " + me.getValue());
25      }
26   }
27   public static HashMap<String, Integer> sortByValue(HashMap<String, Integer> hm)
28   {
29      // creating list from elements of HashMap
30      List<Map.Entry<String, Integer>> list = new LinkedList<Map.Entry<String, Integer>>(hm.entrySet());
31      // sorting list
32      Collections.sort(list, new Comparator<Map.Entry<String, Integer>>()
33      {
34         public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2)
35         {
36            return (o1.getValue()).compareTo(o2.getValue());
37         }
38      });
39      HashMap<String, Integer> ha = new LinkedHashMap<String, Integer>();
40      for(Map.Entry<String, Integer> me : list)
41      {
42         ha.put(me.getKey(), me.getValue());
43      }
44      return ha;
45   }
46}
queries leading to this page
java 8 filter value ascendinghow to sort an array with hashmap in javasort hashmap by value using streamjava sort dictionary by valuehow to sort hashmap on basis of values javasort the map by valuessort a java hashmap according to vauesjava sort map on valuessort items in a map javasorting a map by value javacan you sort a hashmap in javasort map by valuesort map based on value java easy java order hashmap by valuesort a map by value javasort hashmap on value javamap sortsort values in hashmap javasort hashmap valuessort based on value in hashmap javajava map values orderhow to sort hashmap based on values in javasort the map by value in javasort hashmap using valueorder values from map javasort hashmap on the basis of values javajava map with sorted valuessort hashmap by value and keyjava sort map by long valuesorder map by values javajava sort map by value variablesort a map by values in an arrayjava map sort by value then keysort and mapsort hashmao by value in javajava sort map by key and valuejava hashmap sort by key and valuesort map by values in javasorting entries of a hashmapmap sort by value in javasort an hashmap based on the valuessort map by key value javahow to sort the map by value in javahashmap orderby value lowesthow to sort a hashmap by valuejava sort map by valuessort array of hashmap javasort map entry javasort map value in javajava8 sort map with valueshow to sort map by values in javacan we sort hashmap by valuesjava map get sorted valueshow to sort map by values in jvasort map java keyssort map values based in javajava map sort by valuehow to sort elements in map in javajava sorting map by valuesorting a hashmap by value java 7e 7esorting map wrt values in javasorting map by value javajava map create map to order by value map sorted by value javahashmap values sortsort based on value in hashmapjava map sort on valuesort hashmap by value javaorderby map on values javasort by value in hashmap java8sorting hashmap on valueshow to sort map by valuesort elements in hashmap javasorting using hashmaps in javamap sorted by value javasort map by value in java without using collectionssort hashmap based on values javajava sort dictionary by value functionmap java order by valuestore map order by value javasorting map by value in javamap sort by value greatest to least javajava map that sorts by valuesorting in hashmap by valuesort map by valuesjava hashmap sort by valuehow to sort a hashmap using valuesmap in java sorted acc to valuesjava 8 sort map by valuesort map by value in java explainedsort map according to value javajava ordering a mapsort hashmap based on valuessort map by key javahow to sort a dict by values javahow to sort a hashmap in javasort the values of a map in javahow to sort a map by valuejava sort hashmap by key and valuesort elements in map javajava ordering a map according to valuehow to sort a map in java by valuesort map in java by valuesort map using values javahow to sort map with valueshow to sort map according to values javahow to sort map in java using valuessorting a hashmap in jaajava sort dictionary by value variablehow to sort a hashmap by value in javasort map in map javasort hashmap accoring to valuejava sort a map by valuejava hashmap sorted by valuesort map based on value java 8how to sort a map w r t values in javajava map comparator by valuesort map by value stream java map sortsort map javajava sort hashmap by valuessort by value hashmapsort map on the basis of value java 8sorting java map by valuesort map by value in java 8sort a map by value java 8sorting hashmap on the basis of valuesjava sorted map on valuejava sort mapsort a hashmap based on valueshow to sort hashmap in javasort a map by value java8sort a hashmap javasort hashmap by value in java 8java hashmap sort key by valuesort a map with their value javajava sort java util hashmap 24valuessorting hashmaps valuessort hashmap javasorted map by value javasorting a map by values jvaamap sort by value javajava sort map by valuesort map by value java 8sort map entries by value javahashmap sort based on values javacollections sort map by valuesort map by value javaqhow to sort values in a map javasort map with value javajava sort map based on valuekeep map sorted by values javasorting a hashmap by valuejava how to sort a map by valuescan we sort the hashmap values how to sort hashmap javasorting using hashmapmap sort 28 29ordering a hashmap javawrite a program to sort hashmap by value 3fhashmaps for sorting in javajava sort map by value streamshow to sort mapmap sort by valuehow to sort hashmap by values in javasorting using values in hashmapcomparator to sort map in javajava sort java util hashmap by valuessort map based on value in java 7sorting a hasmap javajava created map sort by valuehow to sort a map based on values in javasorting a hashmapsort by value map 2b java8sort items in a mapsort map by key and value javasort hashmap in javamap of map sort by value stream javajava sort map by comparatorhashmap sort by valuehow to sort hashmap value in javasort map based on value javahashmap sorting by string in javasort a hashmap by valuehow to sort hashmap based on valuescan we sort hashmap in javahashmap sorted by valuehashmap sorted by value javasort hashmap by valuejava how to sort a hashmapjava sorting a hashmap by valuessorting an hashmap based on values in javahow to sort the value in hashmapmap sort by keys and value javajava sort hashmap by valuesort a map and return a map in java sorting a map in java by valuehow to sort a map by value in javasorting a hashmap by key in javahow to sort hashmap by valuejava sort map with arraycan i sort hashmap according to values in javasort hashmap by value in javasort java map by valuedoes hashmap sort in javasorting a hashmap implementation javasort map by value javasort map values in java 8java map sorted by valuesort map by value then by key java 8sort value by map javajava how to sort a hashmap by valuesjava sort hashmap with valeshow to sort a hashmap by value javajava sort hashmap by value ascendingsorting hashmap according to valuesjava sort map by values listhashmap sort valuesorting map values in javajava maps with sorting by valuesort the hashmap by valuesort values integer in hash map javasort map by values java 8map of map sort by value javahow to sort hashmap by value in javajava hashmap order by valuehasmmap sort by higest value javasorting an hashmaphashmap sort in javajava sort map with array as valuejava map value as map sortedsort hashmap based on value javasort map based on values javahow to sort map by value in java 8sort map by values in java 8sort the hashmap by valuessort map by value in java 7order a map by value javasort a dictionary by value javajava 8 treemap sort by valuesorting hashmap by value in java 8java order map by valuejava map with comparator by valuemap sort by value java 8sort map by values javasorting of hashmapssorting map by value in java 8sort by value in hashmapjava how to sort map by valuejava map c2 a8sort by valuehashmap sort by value javahashmap sort with valuessort value hashmap javasort map in javahashmap sorting in javahow to sort through the values in map in javamap java order by valuesorting hashmap by values in javasort map values by field javajava map order by valuesorting hashmapsort by value java hashmapsorting a hashmap by value in javahashmap in java sortmap order by value javasorting hashmap by valuehow to sort a map by valuesjava sorting map by keyjava util map sorthow to sort hashmap values in javasorting using hashmap in javasort values in a hashmaphashmap java sort by valuesort values in map javajava map sort based on valuehow to sort a hashmap based on values in javasort the element of hashmap based on the value of valuehow to sort hashmap by value javasort the values in hashmap in javahashmap sortingjava stream order map by valuesorting hashmap in javacan sort the hashmap in javamap compare by value reverse javahow to sort map by value in javajava entry set sort by valuehashmap value sortinghow to sort through map and return highet average in java 8java map sorting by valueorder map by value javahow to sort a map based on valuessorting a map by value in javajava sort hashmaphow to sort hashmap by valuesorder by map on values javahow to sort hashmap by value and then keyjava entryset sort by valuesort hashmap java by valuejava asc ordering a map according to valuesort map by value in javajava sort list by valuesjava stream hashmap sort by valuehow to sort an hashmap in javamap order by value java 8sort by value in javasort hashmap by valuescan i sort hashmap according to valueshow to sort map by value in java 7sort on the basis of value in hashmap javasort hashmap by value java 8sorting hashmap by values in java