How to Sort Map Values by Key in Java

how to sort a map by key when the values are the same?

You could check in your Comparator if the values are the same and if so compare the keys. Here is your adapted method:

public static <K extends Comparable<? super K>, V extends Comparable<? super V>> Map<K, V> newSortMapByValue(Map<K, V> map) {
List<Map.Entry<K, V>> list = new LinkedList<>(map.entrySet());
Collections.sort(list, new Comparator<Map.Entry<K, V>>() {
@Override
public int compare(Map.Entry<K, V> e1, Map.Entry<K, V> e2) {
// Check if values are the same
if (e1.getValue().equals(e2.getValue()))
// Compare e1 to e2, because A should be first element
return e1.getKey().compareTo(e2.getKey());
else
// Compare e2 to e1, because largest number should be first
return e2.getValue().compareTo(e1.getValue());
}
});

Map<K, V> result = new LinkedHashMap<>();
for (Map.Entry<K, V> entry : list) {
result.put(entry.getKey(), entry.getValue());
}
return result;
}

Example main:

    public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("AA",10);
map.put("CC",5);
map.put("BB",5);
map.put("DD",15);
map.put("ZZ",15);

System.out.println(map);
Map sortedMap = newSortMapByValue(map);
System.out.println(sortedMap);
}

Output:

{AA=10, CC=5, BB=5, DD=15, ZZ=15}
{DD=15, ZZ=15, AA=10, BB=5, CC=5}

How get a List of Entries sorted by Value and then by Key from a Map


public class Person implements Comparable<Person>{

private String name;
private Integer age;

public Person(String name, Integer age) {
this.name = name;
this.age = age;
}

@Override
public int compareTo(Person person) {
if(this.age!= person.age){
return person.age.compareTo(this.age);
} else {
return person.name.compareTo(this.name);
}
}

@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}

public static void main(String argsp[]){
Map<String,Integer> data = new HashMap();
data.put("a",10);
data.put("b",3);
data.put("c",10);

List<Person> collect = data.entrySet().stream().map(entry -> new Person(entry.getKey(), entry.getValue())).collect(Collectors.toList());
Collections.sort(collect);
collect.stream().forEach(person-> System.out.println(person));

Collection<Map.Entry<String,Integer>> result = collect.stream().map(person ->
new AbstractMap.SimpleEntry<String, Integer>(person.name, person.age)).collect(Collectors.toList());


}
}

results:

Person{name='c', age=10}
Person{name='a', age=10}
Person{name='b', age=3}

How to sort Map entries by values first, then by key and put the ordered keys in a List?

You could do it that way using functional programming :

final Map<String, Integer> map = new HashMap<>();
map.put("test", 1);
map.put("test1", 3);
map.put("test3", 4);
map.put("test2", 75);
map.put("a", 75);
map.put("test100", 100);

final List<String> test = map
.entrySet()
.stream()
.sorted((Entry<String, Integer> o1, Entry<String, Integer> o2) -> {
return o1.getValue().equals(o2.getValue()) ?
o1.getKey().compareTo(o2.getKey())
: o1.getValue().compareTo(o2.getValue());
})
.map(e -> e.getKey())
.collect(Collectors.toList());

for(String s : test)
System.out.println(s);

This example would output

test test1 test3 a test2 test100



Related Topics



Leave a reply



Submit