adding comparator to Employee
This commit is contained in:
		
							parent
							
								
									e0172002d5
								
							
						
					
					
						commit
						fdf873daf7
					
				| @ -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()); | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -1,6 +1,7 @@ | |||||||
| package com.baeldung.sort; | 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.Lists; | ||||||
| import com.google.common.collect.Ordering; | import com.google.common.collect.Ordering; | ||||||
| 
 | 
 | ||||||
| @ -15,31 +16,24 @@ public class SortHashMap { | |||||||
| 
 | 
 | ||||||
|         initialize(); |         initialize(); | ||||||
| 
 | 
 | ||||||
|         //treeMapSortByKey(); |         treeMapSortByKey(); | ||||||
| 
 | 
 | ||||||
|         //arrayListSortByValue(); |         arrayListSortByValue(); | ||||||
|         //arrayListSortByKey(); |         arrayListSortByKey(); | ||||||
| 
 | 
 | ||||||
|         //treeSetByKey(); |         treeSetByKey(); | ||||||
|         //treeSetByValue(); |         treeSetByValue(); | ||||||
| 
 | 
 | ||||||
|         //sortStream(); |         sortStream(); | ||||||
| 
 | 
 | ||||||
|         sortGuava(); |         sortGuava(); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     private static void sortGuava() { |     private static void sortGuava() { | ||||||
|         Ordering<Map.Entry<String, Employee>> orderById = new Ordering<Map.Entry<String, Employee>>() { |         final Ordering<String> naturalReverseValueOrdering = | ||||||
|             @Override |                 Ordering.natural().reverse().nullsLast().onResultOf(Functions.forMap(map, null)); | ||||||
|             public int compare(Map.Entry<String, Employee> left, Map.Entry<String, Employee> right) { |  | ||||||
|                 return left.getValue().getId().compareTo(right.getValue().getId()); |  | ||||||
|             } |  | ||||||
|         }; |  | ||||||
| 
 | 
 | ||||||
|         List<Map.Entry<String, Employee>> toList = Lists.newArrayList(map.entrySet()); |         System.out.println(ImmutableSortedMap.copyOf(map, naturalReverseValueOrdering)); | ||||||
|         Collections.sort(toList, orderById); |  | ||||||
| 
 |  | ||||||
|         toList.forEach(System.out::println); |  | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     private static void sortStream() { |     private static void sortStream() { | ||||||
| @ -48,7 +42,7 @@ public class SortHashMap { | |||||||
|                 .forEach(System.out::println); |                 .forEach(System.out::println); | ||||||
| 
 | 
 | ||||||
|         Map<String, Employee> result = map.entrySet().stream() |         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, |                 .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, | ||||||
|                 (oldValue, newValue) -> oldValue, LinkedHashMap::new)); |                 (oldValue, newValue) -> oldValue, LinkedHashMap::new)); | ||||||
| 
 | 
 | ||||||
| @ -56,9 +50,7 @@ public class SortHashMap { | |||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     private static void treeSetByValue() { |     private static void treeSetByValue() { | ||||||
|         Comparator<Employee> comp = (Employee o1, Employee o2) -> (o1.getId().compareTo(o2.getId())); |         SortedSet<Employee> values = new TreeSet<>(map.values()); | ||||||
|         SortedSet<Employee> values = new TreeSet<>(comp); |  | ||||||
|         values.addAll(map.values()); |  | ||||||
|         System.out.println(values); |         System.out.println(values); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| @ -81,12 +73,7 @@ public class SortHashMap { | |||||||
|     private static void arrayListSortByValue() { |     private static void arrayListSortByValue() { | ||||||
|         List<Employee> employeeById = new ArrayList<>(map.values()); |         List<Employee> employeeById = new ArrayList<>(map.values()); | ||||||
| 
 | 
 | ||||||
|         Collections.sort(employeeById, new Comparator<Employee>() { |         Collections.sort(employeeById); | ||||||
| 
 |  | ||||||
|             public int compare(Employee o1, Employee o2) { |  | ||||||
|                 return (int)(o1.getId() - o2.getId()); |  | ||||||
|             } |  | ||||||
|         }); |  | ||||||
| 
 | 
 | ||||||
|         System.out.println(employeeById); |         System.out.println(employeeById); | ||||||
|     } |     } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user