-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeSetClass.java
More file actions
47 lines (37 loc) · 1.24 KB
/
TreeSetClass.java
File metadata and controls
47 lines (37 loc) · 1.24 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
package org.simplemedium;
import java.util.Set;
import java.util.TreeSet;
/**
* 22. Create a custom class like Employee/Person with 2 to 3 fields and create the TreeSet and store two objects and display the size of treeset
*/
public class TreeSetClass {
public static void main(String[] args) {
Set<Person> personSet = new TreeSet<Person>();
TreeSetClass selfObj = new TreeSetClass();
personSet.add(selfObj.new Person(1,"Minesh", 41));
personSet.add(selfObj.new Person(2, "XYZ", 33));
personSet.add(selfObj.new Person(3, "PQE", 36));
System.out.println(personSet.size());
for(Person p : personSet) {
System.out.println(p);
}
}
public class Person implements Comparable<Person> {
private int id;
private String name;
private int age;
public Person(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
@Override
public int compareTo(Person o) {
return Integer.compare(this.id, o.id);
}
@Override
public String toString() {
return "Person[id="+id+", name ="+name+", age="+age+"]";
}
}
}