Skip to content

Commit 6fa3ee3

Browse files
authored
Merge pull request eugenp#5237 from MherBaghinyan/BAEL-2180
Bael 2180
2 parents b9970c5 + 3fc968d commit 6fa3ee3

3 files changed

Lines changed: 172 additions & 0 deletions

File tree

‎core-java-collections/src/main/java/com/baeldung/performance/Employee.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,12 @@ public int hashCode() {
4444
result = 31 * result + name.hashCode();
4545
return result;
4646
}
47+
48+
@Override
49+
public String toString() {
50+
return "Employee{" +
51+
"id=" + id +
52+
", name='" + name + '\'' +
53+
'}';
54+
}
4755
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.baeldung.sort;
2+
3+
public class Employee implements Comparable<Employee> {
4+
5+
private Long id;
6+
private String name;
7+
8+
public Employee(Long id, String name) {
9+
this.name = name;
10+
this.id = id;
11+
}
12+
13+
public Long getId() {
14+
return id;
15+
}
16+
17+
public void setId(Long id) {
18+
this.id = id;
19+
}
20+
21+
public String getName() {
22+
return name;
23+
}
24+
25+
public void setName(String name) {
26+
this.name = name;
27+
}
28+
29+
@Override
30+
public boolean equals(Object o) {
31+
if (this == o) return true;
32+
if (o == null || getClass() != o.getClass()) return false;
33+
34+
Employee employee = (Employee) o;
35+
36+
if (!id.equals(employee.id)) return false;
37+
return name.equals(employee.name);
38+
39+
}
40+
41+
@Override
42+
public int hashCode() {
43+
int result = id.hashCode();
44+
result = 31 * result + name.hashCode();
45+
return result;
46+
}
47+
48+
@Override
49+
public String toString() {
50+
return "Employee{" +
51+
"id=" + id +
52+
", name='" + name + '\'' +
53+
'}';
54+
}
55+
56+
@Override
57+
public int compareTo(Employee employee) {
58+
return (int)(this.id - employee.getId());
59+
}
60+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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

Comments
 (0)