-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomObjectCustomizedSortingExample.java
More file actions
71 lines (60 loc) · 1.64 KB
/
CustomObjectCustomizedSortingExample.java
File metadata and controls
71 lines (60 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package lambda_expressions.custom_sorting;
/*
* Program to demonstrate customized sorting of Custom class objects.
*/
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/*
* Comparator is a Functional interface with SAM compare()
* interface<T> Comparator{
* public int compare(T obj1, T obj2);
* }
* return -ve if obj1 has to come before obj2
* return +e if obj1 has to come after obj2
* return 0 if obj1 and obj2 are equal.
*
*/
class Employee{
private int eNo;
private String eName;
Employee(int eNo, String eName){
this.eNo = eNo;
this.eName = eName;
}
public int geteNo() {
return eNo;
}
public void seteNo(int eNo) {
this.eNo = eNo;
}
public String geteName() {
return eName;
}
public void seteName(String eName) {
this.eName = eName;
}
public String toString(){
return eNo+":"+eName;
}
}
public class CustomObjectCustomizedSortingExample {
public static void main(String[] args) {
List<Employee> list = Arrays.asList(
new Employee(100,"Anshuman"),
new Employee(600,"Yuvraj"),
new Employee(300,"Sunny"),
new Employee(200,"Chintu"),
new Employee(700,"Amay"),
new Employee(400,"Pintu")
);
Comparator<Employee> comparator = (e1, e2) -> e2.geteNo() - e1.geteNo();
Collections.sort(list,comparator);
System.out.println(list);
}
}
/*
* Output:
* [700:Amay, 600:Yuvraj, 400:Pintu, 300:Sunny, 200:Chintu, 100:Anshuman]
*/