adding comparator to Employee

This commit is contained in:
mherbaghinyan 2018-09-14 12:12:48 +04:00
parent e0172002d5
commit fdf873daf7
2 changed files with 74 additions and 27 deletions

View File

@ -0,0 +1,60 @@
package com.baeldung.sort;
public class Employee implements Comparable<Employee> {
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());
}
}

View File

@ -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<Map.Entry<String, Employee>> orderById = new Ordering<Map.Entry<String, Employee>>() {
@Override
public int compare(Map.Entry<String, Employee> left, Map.Entry<String, Employee> right) {
return left.getValue().getId().compareTo(right.getValue().getId());
}
};
final Ordering<String> naturalReverseValueOrdering =
Ordering.natural().reverse().nullsLast().onResultOf(Functions.forMap(map, null));
List<Map.Entry<String, Employee>> 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<String, Employee> 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<Employee> comp = (Employee o1, Employee o2) -> (o1.getId().compareTo(o2.getId()));
SortedSet<Employee> values = new TreeSet<>(comp);
values.addAll(map.values());
SortedSet<Employee> values = new TreeSet<>(map.values());
System.out.println(values);
}
@ -81,12 +73,7 @@ public class SortHashMap {
private static void arrayListSortByValue() {
List<Employee> employeeById = new ArrayList<>(map.values());
Collections.sort(employeeById, new Comparator<Employee>() {
public int compare(Employee o1, Employee o2) {
return (int)(o1.getId() - o2.getId());
}
});
Collections.sort(employeeById);
System.out.println(employeeById);
}