From e57bd766456f6954bfd614d0da5a9f04dc47c7c4 Mon Sep 17 00:00:00 2001 From: mherbaghinyan Date: Mon, 10 Sep 2018 16:09:45 +0400 Subject: [PATCH 1/8] HashMap sort TreeMap --- .../com/baeldung/performance/Employee.java | 8 ++++ .../java/com/baeldung/sort/SortHashMap.java | 42 +++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 core-java-collections/src/main/java/com/baeldung/sort/SortHashMap.java diff --git a/core-java-collections/src/main/java/com/baeldung/performance/Employee.java b/core-java-collections/src/main/java/com/baeldung/performance/Employee.java index d811cfbb7d..1ed4410371 100644 --- a/core-java-collections/src/main/java/com/baeldung/performance/Employee.java +++ b/core-java-collections/src/main/java/com/baeldung/performance/Employee.java @@ -44,4 +44,12 @@ public class Employee { result = 31 * result + name.hashCode(); return result; } + + @Override + public String toString() { + return "Employee{" + + "id=" + id + + ", name='" + name + '\'' + + '}'; + } } 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 new file mode 100644 index 0000000000..fe0aab9218 --- /dev/null +++ b/core-java-collections/src/main/java/com/baeldung/sort/SortHashMap.java @@ -0,0 +1,42 @@ +package com.baeldung.sort; + +import com.baeldung.performance.Employee; + +import java.util.HashMap; +import java.util.Map; +import java.util.TreeMap; + +public class SortHashMap { + + private static Map map = new HashMap<>(); + + public static void main(String[] args) { + + initialize(); + + treeMapSortByKey(); + } + + public static void treeMapSortByKey() + { + TreeMap sorted = new TreeMap<>(map); + sorted.putAll(map); + + for (Map.Entry entry : sorted.entrySet()) { + System.out.println("Key = " + entry.getKey() + + ", Value = " + entry.getValue()); + } + + } + + private static void initialize() { + Employee employee1 = new Employee(1L, "Mher"); + map.put(employee1.getName(), employee1); + Employee employee2 = new Employee(22L, "Annie"); + map.put(employee2.getName(), employee2); + Employee employee3 = new Employee(8L, "John"); + map.put(employee3.getName(), employee3); + Employee employee4 = new Employee(2L, "George"); + map.put(employee4.getName(), employee4); + } +} From 23fc41db24562e22d1a88ffa6606b1d7d9e3f5e1 Mon Sep 17 00:00:00 2001 From: mherbaghinyan Date: Mon, 10 Sep 2018 17:22:21 +0400 Subject: [PATCH 2/8] ArrayList and TreeSet --- .../java/com/baeldung/sort/SortHashMap.java | 46 ++++++++++++++++--- 1 file changed, 40 insertions(+), 6 deletions(-) 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 fe0aab9218..6bf56274ac 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 @@ -2,9 +2,7 @@ package com.baeldung.sort; import com.baeldung.performance.Employee; -import java.util.HashMap; -import java.util.Map; -import java.util.TreeMap; +import java.util.*; public class SortHashMap { @@ -14,11 +12,28 @@ public class SortHashMap { initialize(); - treeMapSortByKey(); + //treeMapSortByKey(); + + //arrayListSortByValue(); + //arrayListSortByKey(); + + treeSetByKey(); + treeSetByValue(); } - public static void treeMapSortByKey() - { + private static void treeSetByValue() { + Comparator comp = (Employee o1, Employee o2) -> (o1.getId().compareTo(o2.getId())); + SortedSet values = new TreeSet<>(comp); + values.addAll(map.values()); + System.out.println(values); + } + + private static void treeSetByKey() { + SortedSet keys = new TreeSet<>(map.keySet()); + System.out.println(keys); + } + + private static void treeMapSortByKey() { TreeMap sorted = new TreeMap<>(map); sorted.putAll(map); @@ -29,6 +44,25 @@ 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()); + } + }); + + System.out.println(employeeById); + } + + private static void arrayListSortByKey() { + List employeeByKey = new ArrayList<>(map.keySet()); + Collections.sort(employeeByKey); + System.out.println(employeeByKey); + } + private static void initialize() { Employee employee1 = new Employee(1L, "Mher"); map.put(employee1.getName(), employee1); From 2d15cb3b49bd2ae1ad1f39bc2d87b864538bce4b Mon Sep 17 00:00:00 2001 From: mherbaghinyan Date: Tue, 11 Sep 2018 15:58:47 +0400 Subject: [PATCH 3/8] set benchmark tests --- .../src/main/java/com/baeldung/sort/SortHashMap.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) 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 6bf56274ac..6adb2ee120 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 @@ -17,8 +17,16 @@ public class SortHashMap { //arrayListSortByValue(); //arrayListSortByKey(); - treeSetByKey(); - treeSetByValue(); + //treeSetByKey(); + //treeSetByValue(); + + sortStream(); + } + + private static void sortStream() { + map.entrySet().stream() + .sorted(Map.Entry.comparingByKey().reversed()) + .forEach(System.out::println); } private static void treeSetByValue() { From 6e71cd302cfac6e259693c4d28009023bbf54a3f Mon Sep 17 00:00:00 2001 From: mherbaghinyan Date: Thu, 13 Sep 2018 00:10:16 +0400 Subject: [PATCH 4/8] HashMap sort initial version --- .../java/com/baeldung/sort/SortHashMap.java | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) 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 6adb2ee120..938f740e9a 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,8 +1,11 @@ package com.baeldung.sort; import com.baeldung.performance.Employee; +import com.google.common.collect.Lists; +import com.google.common.collect.Ordering; import java.util.*; +import java.util.stream.Collectors; public class SortHashMap { @@ -20,13 +23,36 @@ public class SortHashMap { //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()); + } + }; + + List> toList = Lists.newArrayList(map.entrySet()); + Collections.sort(toList, orderById); + + toList.forEach(System.out::println); } private static void sortStream() { map.entrySet().stream() .sorted(Map.Entry.comparingByKey().reversed()) .forEach(System.out::println); + + Map 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() { From e0172002d5c171ea2c2dc93b59bfede09b6524dc Mon Sep 17 00:00:00 2001 From: mherbaghinyan Date: Thu, 13 Sep 2018 01:14:09 +0400 Subject: [PATCH 5/8] minor change --- .../src/main/java/com/baeldung/sort/SortHashMap.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 938f740e9a..c803e8f193 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 @@ -63,8 +63,8 @@ public class SortHashMap { } private static void treeSetByKey() { - SortedSet keys = new TreeSet<>(map.keySet()); - System.out.println(keys); + SortedSet keysSet = new TreeSet<>(map.keySet()); + System.out.println(keysSet); } private static void treeMapSortByKey() { From fdf873daf7c718235b75999081f1acb10585ec62 Mon Sep 17 00:00:00 2001 From: mherbaghinyan Date: Fri, 14 Sep 2018 12:12:48 +0400 Subject: [PATCH 6/8] 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); } From 9d9325b972c4e5e5a65283e0b7b6b8ffcbcac8b1 Mon Sep 17 00:00:00 2001 From: Mher Baghinyan Date: Sun, 16 Sep 2018 11:18:22 +0400 Subject: [PATCH 7/8] Update SortHashMap.java --- .../src/main/java/com/baeldung/sort/SortHashMap.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 f7ea2f655b..ce2d0d3c3f 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 @@ -30,10 +30,10 @@ public class SortHashMap { } private static void sortGuava() { - final Ordering naturalReverseValueOrdering = - Ordering.natural().reverse().nullsLast().onResultOf(Functions.forMap(map, null)); + final Ordering naturalOrdering = + Ordering.natural().onResultOf(Functions.forMap(map, null)); - System.out.println(ImmutableSortedMap.copyOf(map, naturalReverseValueOrdering)); + System.out.println(ImmutableSortedMap.copyOf(map, naturalOrdering)); } private static void sortStream() { From 91ea35ee387324346156e3711cb6a4e196d3654e Mon Sep 17 00:00:00 2001 From: mherbaghinyan Date: Sat, 22 Sep 2018 21:15:03 +0400 Subject: [PATCH 8/8] review changes --- .../java/com/baeldung/sort/SortHashMap.java | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) 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 f7ea2f655b..38eddc6cf3 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 @@ -21,12 +21,15 @@ public class SortHashMap { arrayListSortByValue(); arrayListSortByKey(); - treeSetByKey(); - treeSetByValue(); - sortStream(); sortGuava(); + + addDuplicates(); + + treeSetByKey(); + treeSetByValue(); + } private static void sortGuava() { @@ -63,10 +66,7 @@ public class SortHashMap { TreeMap sorted = new TreeMap<>(map); sorted.putAll(map); - for (Map.Entry entry : sorted.entrySet()) { - System.out.println("Key = " + entry.getKey() + - ", Value = " + entry.getValue()); - } + sorted.entrySet().forEach(System.out::println); } @@ -94,4 +94,11 @@ public class SortHashMap { Employee employee4 = new Employee(2L, "George"); map.put(employee4.getName(), employee4); } + + private static void addDuplicates() { + Employee employee5 = new Employee(1L, "Mher"); + map.put(employee5.getName(), employee5); + Employee employee6 = new Employee(22L, "Annie"); + map.put(employee6.getName(), employee6); + } }