Skip to content

Commit fdf873d

Browse files
committed
adding comparator to Employee
1 parent e017200 commit fdf873d

2 files changed

Lines changed: 74 additions & 27 deletions

File tree

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+
}

‎core-java-collections/src/main/java/com/baeldung/sort/SortHashMap.java‎

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.baeldung.sort;
22

3-
import com.baeldung.performance.Employee;
3+
import com.google.common.base.Functions;
4+
import com.google.common.collect.ImmutableSortedMap;
45
import com.google.common.collect.Lists;
56
import com.google.common.collect.Ordering;
67

@@ -15,31 +16,24 @@ public static void main(String[] args) {
1516

1617
initialize();
1718

18-
//treeMapSortByKey();
19+
treeMapSortByKey();
1920

20-
//arrayListSortByValue();
21-
//arrayListSortByKey();
21+
arrayListSortByValue();
22+
arrayListSortByKey();
2223

23-
//treeSetByKey();
24-
//treeSetByValue();
24+
treeSetByKey();
25+
treeSetByValue();
2526

26-
//sortStream();
27+
sortStream();
2728

2829
sortGuava();
2930
}
3031

3132
private static void sortGuava() {
32-
Ordering<Map.Entry<String, Employee>> orderById = new Ordering<Map.Entry<String, Employee>>() {
33-
@Override
34-
public int compare(Map.Entry<String, Employee> left, Map.Entry<String, Employee> right) {
35-
return left.getValue().getId().compareTo(right.getValue().getId());
36-
}
37-
};
33+
final Ordering<String> naturalReverseValueOrdering =
34+
Ordering.natural().reverse().nullsLast().onResultOf(Functions.forMap(map, null));
3835

39-
List<Map.Entry<String, Employee>> toList = Lists.newArrayList(map.entrySet());
40-
Collections.sort(toList, orderById);
41-
42-
toList.forEach(System.out::println);
36+
System.out.println(ImmutableSortedMap.copyOf(map, naturalReverseValueOrdering));
4337
}
4438

4539
private static void sortStream() {
@@ -48,17 +42,15 @@ private static void sortStream() {
4842
.forEach(System.out::println);
4943

5044
Map<String, Employee> result = map.entrySet().stream()
51-
.sorted(Comparator.comparingLong(e -> e.getValue().getId()))
45+
.sorted(Map.Entry.comparingByValue())
5246
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
5347
(oldValue, newValue) -> oldValue, LinkedHashMap::new));
5448

5549
result.entrySet().forEach(System.out::println);
5650
}
5751

5852
private static void treeSetByValue() {
59-
Comparator<Employee> comp = (Employee o1, Employee o2) -> (o1.getId().compareTo(o2.getId()));
60-
SortedSet<Employee> values = new TreeSet<>(comp);
61-
values.addAll(map.values());
53+
SortedSet<Employee> values = new TreeSet<>(map.values());
6254
System.out.println(values);
6355
}
6456

@@ -81,12 +73,7 @@ private static void treeMapSortByKey() {
8173
private static void arrayListSortByValue() {
8274
List<Employee> employeeById = new ArrayList<>(map.values());
8375

84-
Collections.sort(employeeById, new Comparator<Employee>() {
85-
86-
public int compare(Employee o1, Employee o2) {
87-
return (int)(o1.getId() - o2.getId());
88-
}
89-
});
76+
Collections.sort(employeeById);
9077

9178
System.out.println(employeeById);
9279
}

0 commit comments

Comments
 (0)