java treemap sort by value in reverse
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:
- update value of an arraylist java
- Caused by: java.lang.NumberFormatException: Invalid int: ""
- java hashmap example
- java quit application
- java if different ways
- java loop through object
- get last element of array java
- java read from connection even if 404
- BoxLayout java
- convert string to int java
- java binary exponentiation
- print map java
- how to install java runtime environment on centos 7
- android studio clear views of layout
- time conversion solution
- how to use the this keyword in java
- how to select a random element from an array in java
- java processbuilder example
- map with insertion order preserved java
- creating thread in java example
- The shrinker may have failed to optimize the Java bytecode. To disable the shrinker, pass the `--no-shrink` flag to this command.
- how to import an arraylist in java
- how to open a folder in java swing project
- java jpa criteriabuilder in xample