-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyEmployee_Comparable-Comparator.java
89 lines (64 loc) · 1.29 KB
/
MyEmployee_Comparable-Comparator.java
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import java.lang.*;
import java.util.*;
class MyEmployee_Comparable_Comparator
{
public static void main(String[] args)
{
Employee e1=new Employee(10,"Deep");
Employee e2=new Employee(3,"Shivam");
Employee e3=new Employee(6,"pratik");
Employee e4=new Employee(1,"Anjali");
Employee e5=new Employee(2,"Arti");
TreeSet t=new TreeSet();
t.add(e1);
t.add(e2);
t.add(e3);
t.add(e4);
t.add(e5);
System.out.println(t);
TreeSet t1=new TreeSet(new MyComparator());
t1.add(e1);
t1.add(e2);
t1.add(e3);
t1.add(e4);
t1.add(e5);
System.out.println(t1);
}
}
class Employee implements Comparable
{
int eid;
String name;
Employee(int eid, String name)
{
this.eid=eid;
this.name=name;
}
public String toString()
{
return (eid+"---"+ name);
}
public int/*(obj1)*/ compareTo(Object obj/*(obj2)*/)
{
int eid1=this.eid;
Employee e=(Employee)obj;
int eid2=e.eid;
if(eid1<eid2)
return 1;
else if(eid1>eid2)
return -1;
else
return 0;
}
}
class MyComparator implements Comparator
{
public int compare(Object obj1,Object obj2)
{
Employee e1=(Employee)obj1;
Employee e2=(Employee)obj2;
String s1=e1.name;
String s2=e2.name;
return s1.compareTo(s2);
}
}