-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtreeSetExample.java
More file actions
47 lines (39 loc) · 1.36 KB
/
treeSetExample.java
File metadata and controls
47 lines (39 loc) · 1.36 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
import java.util.TreeSet;
class EmployeeAgain{
int salary;
String name;
public EmployeeAgain(String name,int salary) {
this.name=name;
this.salary=salary;
}
@Override
public String toString() {
return "Name: "+name+" Salary: "+salary;
}
}
public class treeSetExample {
public static void main(String[] args) {
TreeSet ts = new TreeSet((o1,o2)->{
EmployeeAgain i1 =(EmployeeAgain)o1;
EmployeeAgain i2 =(EmployeeAgain)o2;
return (Integer)(i2.salary-i1.salary);
});
ts.add(new EmployeeAgain("Hello", 989832));
ts.add(new EmployeeAgain("Hgdd", 989843));
ts.add(new EmployeeAgain("Hreewree", 1982333223));
ts.add(new EmployeeAgain("Hefe", 982333223));
System.out.println("Output: "+ts);
// Another Approach
// System.out.println("Another Approach: ");
// TreeSet ts1 = new TreeSet();
// Comparator cp = (o1,o2)->{
// EmployeeAgain em1 = (EmployeeAgain)o1;
// EmployeeAgain em2 = (EmployeeAgain)o2;
// return (int)(em1.salary-em2.salary);
// };
// ts1.add(new EmployeeAgain("Wow", 9832));
// ts1.add(new EmployeeAgain("Wow2", 92));
// ts1.add(new EmployeeAgain("Wow3", 982));
// System.out.println("Output New :"+ts1);
}
}