/
TreeSetDemo.java
58 lines (51 loc) · 1.38 KB
/
TreeSetDemo.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
import java.util.Comparator;
import java.util.TreeSet;
class Customer implements Comparable<Customer>{
private int id;
private String name;
public int compareTo(Customer customer){
return this.name.compareToIgnoreCase(customer.name);
}
Customer(int id , String name){
this.id = id ;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString(){
return "id "+id+" Name "+name;
}
}
class SortByCustomerId implements Comparator<Customer>{
public int compare(Customer first , Customer second){
return ((Integer)first.getId()).compareTo((Integer)second.getId());
}
}
public class TreeSetDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
//TreeSet<Customer> customerSet = new TreeSet<>();
// Internally it use Comparable
//TreeSet<Customer> customerSet= new TreeSet<>(new SortByCustomerId());
TreeSet<Customer> customerSet = new TreeSet<>
((first,second)->((Integer)first.getId()).compareTo(second.getId()));
Customer ram = new Customer(1001,"Ram");
Customer ram2 = new Customer(1001,"Ram");
Customer shyam = new Customer(1000,"Shyam");
customerSet.add(ram);
customerSet.add(ram2);
customerSet.add(shyam);
System.out.println(customerSet);
}
}