Java sort Map by values

Map<String, Integer> unSortedMap = getUnSortedMap();
         
System.out.println("Unsorted Map : " + unSortedMap);
 
//LinkedHashMap preserve the ordering of elements in which they are inserted
LinkedHashMap<String, Integer> sortedMap = new LinkedHashMap<>();
 
unSortedMap.entrySet()
    .stream()
    .sorted(Map.Entry.comparingByValue())
    .forEachOrdered(x -> sortedMap.put(x.getKey(), x.getValue()));
 
System.out.println("Sorted Map   : " + sortedMap);
 
Output:
 
Unsorted Map : {alex=1, charles=4, david=2, brian=5, elle=3}
Sorted Map   : {alex=1, david=2, elle=3, charles=4, brian=5}

4.67
3

                                    //----------------------------------------------------------------------
// in ascending order:
Map&lt;String, Integer&gt; unSortedMap = getUnSortedMap();
         
System.out.println(&quot;Unsorted Map : &quot; + unSortedMap);
 
//LinkedHashMap preserve the ordering of elements in which they are inserted
LinkedHashMap&lt;String, Integer&gt; sortedMap = new LinkedHashMap&lt;&gt;();
 
unSortedMap.entrySet()
    .stream()
    .sorted(Map.Entry.comparingByValue())
    .forEachOrdered(x -&gt; sortedMap.put(x.getKey(), x.getValue()));
 
System.out.println(&quot;Sorted Map   : &quot; + sortedMap);
 
Output:
 
Unsorted Map : {alex=1, charles=4, david=2, brian=5, elle=3}
Sorted Map   : {alex=1, david=2, elle=3, charles=4, brian=5}


//----------------------------------------------------------------------
// in descending order:
Map&lt;String, Integer&gt; unSortedMap = getUnSortedMap();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
System.out.println(&quot;Unsorted Map : &quot; + unSortedMap);
&nbsp;
//LinkedHashMap preserve the ordering of elements in which they are inserted
LinkedHashMap&lt;String, Integer&gt; reverseSortedMap = new LinkedHashMap&lt;&gt;();
&nbsp;
//Use Comparator.reverseOrder() for reverse ordering
unSortedMap.entrySet()
&nbsp;&nbsp;&nbsp;&nbsp;.stream()
&nbsp;&nbsp;&nbsp;&nbsp;.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())) 
&nbsp;&nbsp;&nbsp;&nbsp;.forEachOrdered(x -&gt; reverseSortedMap.put(x.getKey(), x.getValue()));
&nbsp;
System.out.println(&quot;Reverse Sorted Map&nbsp;&nbsp; : &quot; + reverseSortedMap);
&nbsp;
Output:
&nbsp;
Unsorted Map&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : {alex=1, charles=4, david=2, brian=5, elle=3}
Reverse Sorted Map&nbsp; : {brian=5, charles=4, elle=3, david=2, alex=1}

4.67 (3 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
sort map by value in java without using collections sort map values in java 8 how to sort map according to values java how to sort map by value in java 8 java treemap sorted by value sorting a map in java by value sort map by value in java 7 sorting map by value in java 8 how to sort map by value in java 7 how to sort a map by value sort map based on value java easy sort the Map by value in reverse order in java can treemap sort on value in java sorting map values in java java sorting map by key sort a map and return a map in java sort map entry java sort a map with their value java sort treemap by value in java sort map with value java map of map sort by value stream java map of map sort by value java sort map by value stream java treemap sort by value java sort the map by value in java java sort hashmap by values java map sort on value java8 sort Map with values sorting map by value java order by map on values java orderby map on values java sorting map by value in java map sorted by value java how to sort a map by values map sort by value in java sorting map wrt values in java order a map by value java sort map values by field java sort map by values java 8 sort map on the basis of value java 8 sort by value map + java8 sort map using values java how to sort a map in java by value how to sort a treemap by value in java how to sort treemap in descending order in java how to sort treemap by value in java java sorted map on value sort values in map java sort map by value in java explained order map by value java sort map by key and value java java map&uml;sort by value map sorted by value java sort treemap in descending order by value java sort treemap by value java map sorted by value sort map by values java sort map by values List order values from map java sort map in map java how to sort through the values in map in java how to sort map by value how to sort the map by value in java sorting java map by value order map by values java java sorting map by value hashmap sort based on values java treemap sort on value java sort map by value streams map sort by keys AND value java java sort map by key and value sort map based on value java 8 sort the values of a map in java java map values order sort map based on values java java sort map on values collections sort map by value sorting a map by values jvaa sort map value in java java treemap sort by value Sorting HashMap by values in java java sort list by values sort values integer in hash map java how to sort a map based on values sort map by key java sort map by value javaq java map sort based on value sort items in a map java sort items in a map java sort map by long values java map that sorts by value how to sort values in a map java sort map java keys java map with sorted values sort map according to value java comparator to sort map in java java sort map java sort map based on value sort map by key value java java sort map by comparator java entry set sort by value java sort map with array java sort map with array as value map sort by value greatest to least java how to sort map sort a map by values in an array map sort Sort Map by values java store map order by value java java ordering a map java map get sorted values java util map sort .map sort java order map by value sort map by values in java 8 java entryset sort by value map sort by value java 8 sort map by value java 8 hashmap orderby value lowest sorted map by value java treemap sort by value descending sort a dictionary by value java map order by value java sort a map by value java8 sort a map by value java 8 java map create map to order by value java created map sort by value java 8 sort map by value sort map by value HasmMap sort by higest value java sort map by values in java java map with comparator by value java map comparator by value java sort map by value variable java stream order map by value java sort dictionary by value variable java sort dictionary by value function java sort dictionary by value java sort a map by value how to sort a dict by values java sorting a map by value in java how to sort through map and return highet average in java 8 map compare by value reverse java java how to sort map by value how to sort map by value in java java 8 filter value ascending sort map entries by value java java Maps with sorting by value java asc ordering a map according to value java ordering a map according to value sort map by value in java 8 map order by value java 8 java map sort by value descending map java order by value map java order by value java 8 treemap sort by value sort by value in java sort map by value in java Java how to sort a map by values map sort by value java map sort by value how to sort map in java using values sort and map map sort () how to sort a map by value in java sorting an hashmap based on values in java how to sort array in descending order in c++ java map order by value java sort map by value sort a map by value java java map sort by value Java sort Map by values sort map by value java java treemap sort by value descending java treemap sort by value in reverse
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source