HashMap sort initial version
This commit is contained in:
parent
2d15cb3b49
commit
6e71cd302c
@ -1,8 +1,11 @@
|
|||||||
package com.baeldung.sort;
|
package com.baeldung.sort;
|
||||||
|
|
||||||
import com.baeldung.performance.Employee;
|
import com.baeldung.performance.Employee;
|
||||||
|
import com.google.common.collect.Lists;
|
||||||
|
import com.google.common.collect.Ordering;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class SortHashMap {
|
public class SortHashMap {
|
||||||
|
|
||||||
@ -20,13 +23,36 @@ public class SortHashMap {
|
|||||||
//treeSetByKey();
|
//treeSetByKey();
|
||||||
//treeSetByValue();
|
//treeSetByValue();
|
||||||
|
|
||||||
sortStream();
|
//sortStream();
|
||||||
|
|
||||||
|
sortGuava();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void sortGuava() {
|
||||||
|
Ordering<Map.Entry<String, Employee>> orderById = new Ordering<Map.Entry<String, Employee>>() {
|
||||||
|
@Override
|
||||||
|
public int compare(Map.Entry<String, Employee> left, Map.Entry<String, Employee> right) {
|
||||||
|
return left.getValue().getId().compareTo(right.getValue().getId());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
List<Map.Entry<String, Employee>> toList = Lists.newArrayList(map.entrySet());
|
||||||
|
Collections.sort(toList, orderById);
|
||||||
|
|
||||||
|
toList.forEach(System.out::println);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void sortStream() {
|
private static void sortStream() {
|
||||||
map.entrySet().stream()
|
map.entrySet().stream()
|
||||||
.sorted(Map.Entry.<String, Employee>comparingByKey().reversed())
|
.sorted(Map.Entry.<String, Employee>comparingByKey().reversed())
|
||||||
.forEach(System.out::println);
|
.forEach(System.out::println);
|
||||||
|
|
||||||
|
Map<String, Employee> result = map.entrySet().stream()
|
||||||
|
.sorted(Comparator.comparingLong(e -> e.getValue().getId()))
|
||||||
|
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
|
||||||
|
(oldValue, newValue) -> oldValue, LinkedHashMap::new));
|
||||||
|
|
||||||
|
result.entrySet().forEach(System.out::println);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void treeSetByValue() {
|
private static void treeSetByValue() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user