Java sort Map by values
Java
//----------------------------------------------------------------------
// in ascending order:
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}
//----------------------------------------------------------------------
// in descending order:
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> reverseSortedMap = new LinkedHashMap<>();
//Use Comparator.reverseOrder() for reverse ordering
unSortedMap.entrySet()
.stream()
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
.forEachOrdered(x -> reverseSortedMap.put(x.getKey(), x.getValue()));
System.out.println("Reverse Sorted Map : " + reverseSortedMap);
Output:
Unsorted Map : {alex=1, charles=4, david=2, brian=5, elle=3}
Reverse Sorted Map : {brian=5, charles=4, elle=3, david=2, alex=1}
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}
Also in Java:
- Title
- get value textfield java
- Category
- Java
- Title
- write an infinite loop java
- Category
- Java
- Title
- replace all punctuation in string java
- Category
- Java
- Title
- queue implementation in java using arraylist
- Category
- Java
- Title
- calculating the percentile in java
- Category
- Java
- Title
- para que sirve getcontentpane en java
- Category
- Java
- Title
- java stream sort
- Category
- Java
- Title
- int to binary string java
- Category
- Java
- Title
- pretty print json in console
- Category
- Java
- Title
- java replaceall single character
- Category
- Java
- Title
- how to play a clip of audio in java when needed
- Category
- Java
- Title
- déclarer un tableau en java
- Category
- Java
- Title
- query java persistence
- Category
- Java
- Title
- write an object java in a file
- Category
- Java
- Title
- Java array hashcode
- Category
- Java
- Title
- compile java to jar in mac
- Category
- Java
- Title
- how to create an array with unique values
- Category
- Java
- Title
- java map print
- Category
- Java
- Title
- add element to stack java
- Category
- Java
- Title
- how to sort collection in java
- Category
- Java
- Title
- android get distance between two locations kotlin
- Category
- Java
- Title
- not equal to java
- Category
- Java
- Title
- how to show the hex detail of a file in java
- Category
- Java
- Title
- java output length of each line in a text document
- Category
- Java
- Title
- how to create a constructor in java
- Category
- Java
- Title
- diagonal difference hackerrank solution in java 8 using list
- Category
- Java
- Title
- java create directory
- Category
- Java
- Title
- add two numbers bitwise
- Category
- Java
- Title
- kotlin android intent pass data
- Category
- Java
- Title
- spigot run task later
- Category
- Java
- Title
- java swing draw centered text
- Category
- Java
- Title
- how to fill a 2d array in java
- Category
- Java
- Title
- online money transfer andhra bank
- Category
- Java
- Title
- how to check if a list is empty java
- Category
- Java
- Title
- parseints(str) java
- Category
- Java
- Title
- print a letter in java
- Category
- Java
- Title
- java method overriding
- Category
- Java
- Title
- socket programming in java
- Category
- Java
- Title
- java read from file
- Category
- Java
- Title
- java packages example
- Category
- Java
- Title
- room insert and return id
- Category
- Java
- Title
- how to skip a line in java
- Category
- Java
- Title
- how to stop a void java
- Category
- Java
- Title
- difference between compile and execute in java
- Category
- Java
- Title
- java split string
- Category
- Java
- Title
- Java array nested equals
- Category
- Java
- Title
- java loop through map
- Category
- Java
- Title
- in dom parser how to find processing instruction in java
- Category
- Java
- Title
- how do i print text in java
- Category
- Java
- Title
- show menuitem when menu hovered javafx
- Category
- Java
- Title
- java jagged array days and months
- Category
- Java
- Title
- how to delete an element from an array in java
- Category
- Java
- Title
- raise number to power java
- Category
- Java
- Title
- java random number in range
- Category
- Java
- Title
- java literals w3schools
- Category
- Java
- Title
- java sort method
- Category
- Java
- Title
- java pass arraylist by value
- Category
- Java
- Title
- java read lines from file
- Category
- Java
- Title
- java get command line input
- Category
- Java
- Title
- java 8 map foreach
- Category
- Java
- Title
- override java
- Category
- Java
- Title
- palindrome function java
- Category
- Java
- Title
- how to fill a list with a single value java
- Category
- Java
- Title
- insert element into arraylist java
- Category
- Java
- Title
- java loop through string
- Category
- Java
- Title
- @fortawesome/free-solid-svg-icons
- Category
- Java
- Title
- how to get all of the chldren of a panel in java
- Category
- Java
- Title
- java public static void main
- Category
- Java
- Title
- how to import an arraylist in java
- Category
- Java
- Title
- File Parser java
- Category
- Java
- Title
- java get int from double without rounding
- Category
- Java
- Title
- spigot run command as console
- Category
- Java
- Title
- parsedouble java
- Category
- Java
- Title
- kotlin vs java
- Category
- Java
- Title
- treemap get order java
- Category
- Java
- Title
- java string array to arraylist
- Category
- Java
- Title
- could not initialize defaultkotlinsourcesetkt intelij
- Category
- Java
- Title
- index of an array procesing
- Category
- Java
- Title
- Write a method that raises a number to a power without using Math.pow() method
- Category
- Java
- Title
- firebase connecten
- Category
- Java
- Title
- priority queue in java
- Category
- Java
- Title
- how to select a element in an arraylist java
- Category
- Java
- Title
- java how to call getReader twice
- Category
- Java
- Title
- java io
- Category
- Java
- Title
- teimpo en segundos java
- Category
- Java
- Title
- Java use Base64
- Category
- Java
- Title
- delete element from arraylist
- Category
- Java
- Title
- how to get binary value in java
- Category
- Java
- Title
- java cast duration to long
- Category
- Java
- Title
- read csv in java in spring
- Category
- Java
- Title
- java cannot find file path
- Category
- Java
- Title
- properties java 8 maven in pom xml
- Category
- Java
- Title
- arraylist to array java
- Category
- Java
- Title
- random java
- Category
- Java
- Title
- java 8 string to localdate
- Category
- Java
- Title
- countdown timer with seekbar
- Category
- Java
- Title
- what is java plug-in
- Category
- Java
- Title
- StringBuilder java pop back
- Category
- Java
- Title
- time conversion solution
- Category
- Java
- Title
- working with buttons in applet java
- Category
- Java