How can I sort a LinkedHashMap<String, Integer> based on its values?.
I need to sort it based on the values which are Integers.
How can I sort a LinkedHashMap<String, Integer> based on its values?.
I need to sort it based on the values which are Integers.
List<Map.Entry<String, Integer>> entries =
new ArrayList<Map.Entry<String, Integer>>(map.entrySet());
Collections.sort(entries, new Comparator<Map.Entry<String, Integer>>() {
public int compare(Map.Entry<String, Integer> a, Map.Entry<String, Integer> b){
return a.getValue().compareTo(b.getValue());
}
});
Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
for (Map.Entry<String, Integer> entry : entries) {
sortedMap.put(entry.getKey(), entry.getValue());
}
Collections.reverseOrder(comparator) which makes the meaning more obvious.This is now quite a bit easier with Java 8 streams: you don't need the intermediate map to sort:
map.entrySet().stream()
.sorted(Map.Entry.comparingByValue())
.forEach(entry -> ... );
LinkedHashMap (the type of map mentioned in the comment) absolutely could be sorted in place — stackoverflow.com/a/79694728/1108305, so such an interpretation is sensible. That said, "using the entries in order of their values" is probably the more appropriate solution in most cases.putLast with it. Then continue to repeat this for the unsorted portion.LinkedHashMap just maintains insertion order. If you want to sort based on value, you may need to write your own comparator.
Map.Entry.comparingByValue() should generate one for you. And it can be reversed by Collections.reverseOrder(Map.Entry.comparingByValue()).Comparable or provide a customer Comparator to Map.Entry.comparingByValue.import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.NavigableMap;
import java.util.SortedMap;
import java.util.TreeMap;
public class HashMapTest {
public static void main(String[] args) {
Map<String, Integer> map=new LinkedHashMap<String, Integer>();
map.put("a", 11);
map.put("B", 12);
map.put("c", 3);
map.put("d", 4);
map.put("e", 5);
map.put("f", 6);
map.put("g", 7);
map.put("h", 8);
map.put("i", 9);
map.put("j", 3);
map.put("k", 2);
map.put("l", 1);
List<Map.Entry<String, Integer>> entries = new
ArrayList<Map.Entry<String, Integer>>(map.entrySet());
Collections.sort(entries,new CustomizedHashMap());
Map<String, Integer> sortedMap = new LinkedHashMap<String,
Integer>();
for (Map.Entry<String, Integer> entry : entries) {
sortedMap.put(entry.getKey(), entry.getValue());
System.out.print( sortedMap.put(entry.getKey(),
entry.getValue())+" ");
}
}
}
class CustomizedHashMap implements Comparator<Map.Entry<String, Integer>> {
@Override
public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
// TODO Auto-generated method stub
return -o1.getValue().compareTo(o2.getValue());
}
}
new CustomizedHashMap() can be replaced by Map.Entry.comparingByValue().reversed(). Also, CustomizedHashMap sounds like it would be a Map implementation, not a Comparator implementation. Those specifics aside, this is definitely a sensible approach.It is possible to sort the LinkedHashMap in place (as a strict reading of your question suggests you are asking for).
First create a sorted copy of the data, then reorder the original map by calling its putLast method with each sorted element. The result of this will be the original map in the desired sorted order.
LinkedHashMap<String, Integer> map = getMyLinkedHashMap();
List<Map.Entry<String, Integer>> sortedEntries = map.entrySet().stream()
.map(Map.Entry::copyOf) // Use AbstractMap.SimpleImmutableEntry instead if key or value may be null
.sorted(Map.Entry.comparingByValue())
.toList();
sortedEntries.forEach(e -> map.putLast(e.getKey(), e.getValue()));
Example:
LinkedHashMap<String, Integer> map = new LinkedHashMap<>();
map.put("A", 1);
map.put("B", 2);
map.put("C", 1);
map.put("D", 4);
map.put("E", 0);
List<Map.Entry<String, Integer>> sortedEntries = map.entrySet().stream()
.map(Map.Entry::copyOf)
.sorted(Map.Entry.comparingByValue())
.toList();
sortedEntries.forEach(e -> map.putLast(e.getKey(), e.getValue()));
System.out.println(map); // {E=0, A=1, C=1, B=2, D=4}