From fdf873daf7c718235b75999081f1acb10585ec62 Mon Sep 17 00:00:00 2001 From: mherbaghinyan Date: Fri, 14 Sep 2018 12:12:48 +0400 Subject: [PATCH] adding comparator to Employee --- .../main/java/com/baeldung/sort/Employee.java | 60 +++++++++++++++++++ .../java/com/baeldung/sort/SortHashMap.java | 41 +++++-------- 2 files changed, 74 insertions(+), 27 deletions(-) create mode 100644 core-java-collections/src/main/java/com/baeldung/sort/Employee.java diff --git a/core-java-collections/src/main/java/com/baeldung/sort/Employee.java b/core-java-collections/src/main/java/com/baeldung/sort/Employee.java new file mode 100644 index 0000000000..b5e56f6141 --- /dev/null +++ b/core-java-collections/src/main/java/com/baeldung/sort/Employee.java @@ -0,0 +1,60 @@ +package com.baeldung.sort; + +public class Employee implements Comparable { + + private Long id; + private String name; + + public Employee(Long id, String name) { + this.name = name; + this.id = id; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Employee employee = (Employee) o; + + if (!id.equals(employee.id)) return false; + return name.equals(employee.name); + + } + + @Override + public int hashCode() { + int result = id.hashCode(); + result = 31 * result + name.hashCode(); + return result; + } + + @Override + public String toString() { + return "Employee{" + + "id=" + id + + ", name='" + name + '\'' + + '}'; + } + + @Override + public int compareTo(Employee employee) { + return (int)(this.id - employee.getId()); + } +} diff --git a/core-java-collections/src/main/java/com/baeldung/sort/SortHashMap.java b/core-java-collections/src/main/java/com/baeldung/sort/SortHashMap.java index c803e8f193..f7ea2f655b 100644 --- a/core-java-collections/src/main/java/com/baeldung/sort/SortHashMap.java +++ b/core-java-collections/src/main/java/com/baeldung/sort/SortHashMap.java @@ -1,6 +1,7 @@ package com.baeldung.sort; -import com.baeldung.performance.Employee; +import com.google.common.base.Functions; +import com.google.common.collect.ImmutableSortedMap; import com.google.common.collect.Lists; import com.google.common.collect.Ordering; @@ -15,31 +16,24 @@ public class SortHashMap { initialize(); - //treeMapSortByKey(); + treeMapSortByKey(); - //arrayListSortByValue(); - //arrayListSortByKey(); + arrayListSortByValue(); + arrayListSortByKey(); - //treeSetByKey(); - //treeSetByValue(); + treeSetByKey(); + treeSetByValue(); - //sortStream(); + sortStream(); sortGuava(); } private static void sortGuava() { - Ordering> orderById = new Ordering>() { - @Override - public int compare(Map.Entry left, Map.Entry right) { - return left.getValue().getId().compareTo(right.getValue().getId()); - } - }; + final Ordering naturalReverseValueOrdering = + Ordering.natural().reverse().nullsLast().onResultOf(Functions.forMap(map, null)); - List> toList = Lists.newArrayList(map.entrySet()); - Collections.sort(toList, orderById); - - toList.forEach(System.out::println); + System.out.println(ImmutableSortedMap.copyOf(map, naturalReverseValueOrdering)); } private static void sortStream() { @@ -48,7 +42,7 @@ public class SortHashMap { .forEach(System.out::println); Map result = map.entrySet().stream() - .sorted(Comparator.comparingLong(e -> e.getValue().getId())) + .sorted(Map.Entry.comparingByValue()) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (oldValue, newValue) -> oldValue, LinkedHashMap::new)); @@ -56,9 +50,7 @@ public class SortHashMap { } private static void treeSetByValue() { - Comparator comp = (Employee o1, Employee o2) -> (o1.getId().compareTo(o2.getId())); - SortedSet values = new TreeSet<>(comp); - values.addAll(map.values()); + SortedSet values = new TreeSet<>(map.values()); System.out.println(values); } @@ -81,12 +73,7 @@ public class SortHashMap { private static void arrayListSortByValue() { List employeeById = new ArrayList<>(map.values()); - Collections.sort(employeeById, new Comparator() { - - public int compare(Employee o1, Employee o2) { - return (int)(o1.getId() - o2.getId()); - } - }); + Collections.sort(employeeById); System.out.println(employeeById); }