|
| 1 | +package com.baeldung.sort; |
| 2 | + |
| 3 | +import com.google.common.base.Functions; |
| 4 | +import com.google.common.collect.ImmutableSortedMap; |
| 5 | +import com.google.common.collect.Lists; |
| 6 | +import com.google.common.collect.Ordering; |
| 7 | + |
| 8 | +import java.util.*; |
| 9 | +import java.util.stream.Collectors; |
| 10 | + |
| 11 | +public class SortHashMap { |
| 12 | + |
| 13 | + private static Map<String, Employee> map = new HashMap<>(); |
| 14 | + |
| 15 | + public static void main(String[] args) { |
| 16 | + |
| 17 | + initialize(); |
| 18 | + |
| 19 | + treeMapSortByKey(); |
| 20 | + |
| 21 | + arrayListSortByValue(); |
| 22 | + arrayListSortByKey(); |
| 23 | + |
| 24 | + sortStream(); |
| 25 | + |
| 26 | + sortGuava(); |
| 27 | + |
| 28 | + addDuplicates(); |
| 29 | + |
| 30 | + treeSetByKey(); |
| 31 | + treeSetByValue(); |
| 32 | + |
| 33 | + } |
| 34 | + |
| 35 | + private static void sortGuava() { |
| 36 | + final Ordering naturalOrdering = |
| 37 | + Ordering.natural().onResultOf(Functions.forMap(map, null)); |
| 38 | + |
| 39 | + System.out.println(ImmutableSortedMap.copyOf(map, naturalOrdering)); |
| 40 | + } |
| 41 | + |
| 42 | + private static void sortStream() { |
| 43 | + map.entrySet().stream() |
| 44 | + .sorted(Map.Entry.<String, Employee>comparingByKey().reversed()) |
| 45 | + .forEach(System.out::println); |
| 46 | + |
| 47 | + Map<String, Employee> result = map.entrySet().stream() |
| 48 | + .sorted(Map.Entry.comparingByValue()) |
| 49 | + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, |
| 50 | + (oldValue, newValue) -> oldValue, LinkedHashMap::new)); |
| 51 | + |
| 52 | + result.entrySet().forEach(System.out::println); |
| 53 | + } |
| 54 | + |
| 55 | + private static void treeSetByValue() { |
| 56 | + SortedSet<Employee> values = new TreeSet<>(map.values()); |
| 57 | + System.out.println(values); |
| 58 | + } |
| 59 | + |
| 60 | + private static void treeSetByKey() { |
| 61 | + SortedSet<String> keysSet = new TreeSet<>(map.keySet()); |
| 62 | + System.out.println(keysSet); |
| 63 | + } |
| 64 | + |
| 65 | + private static void treeMapSortByKey() { |
| 66 | + TreeMap<String, Employee> sorted = new TreeMap<>(map); |
| 67 | + sorted.putAll(map); |
| 68 | + |
| 69 | + sorted.entrySet().forEach(System.out::println); |
| 70 | + |
| 71 | + } |
| 72 | + |
| 73 | + private static void arrayListSortByValue() { |
| 74 | + List<Employee> employeeById = new ArrayList<>(map.values()); |
| 75 | + |
| 76 | + Collections.sort(employeeById); |
| 77 | + |
| 78 | + System.out.println(employeeById); |
| 79 | + } |
| 80 | + |
| 81 | + private static void arrayListSortByKey() { |
| 82 | + List<String> employeeByKey = new ArrayList<>(map.keySet()); |
| 83 | + Collections.sort(employeeByKey); |
| 84 | + System.out.println(employeeByKey); |
| 85 | + } |
| 86 | + |
| 87 | + private static void initialize() { |
| 88 | + Employee employee1 = new Employee(1L, "Mher"); |
| 89 | + map.put(employee1.getName(), employee1); |
| 90 | + Employee employee2 = new Employee(22L, "Annie"); |
| 91 | + map.put(employee2.getName(), employee2); |
| 92 | + Employee employee3 = new Employee(8L, "John"); |
| 93 | + map.put(employee3.getName(), employee3); |
| 94 | + Employee employee4 = new Employee(2L, "George"); |
| 95 | + map.put(employee4.getName(), employee4); |
| 96 | + } |
| 97 | + |
| 98 | + private static void addDuplicates() { |
| 99 | + Employee employee5 = new Employee(1L, "Mher"); |
| 100 | + map.put(employee5.getName(), employee5); |
| 101 | + Employee employee6 = new Employee(22L, "Annie"); |
| 102 | + map.put(employee6.getName(), employee6); |
| 103 | + } |
| 104 | +} |
0 commit comments