Skip to content

Commit 921555f

Browse files
committed
Compare User define class in TreeSet
1 parent c9105fb commit 921555f

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

Collection/15_TreeSet.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,14 @@ public static void main(String[] args) {
5151

5252
// 12) public E higher(E);
5353
System.out.println(ts.higher("Ashish"));
54-
// 13) public E pollFirst();
54+
55+
// 13) public java.util.NavigableSet<E> descendingSet();
56+
System.out.println(ts.descendingSet());
57+
// 14) public E pollFirst();
5558
System.out.println(ts.pollFirst());
5659
System.out.println(ts);
5760

58-
// 14) public E pollLast()
61+
// 15) public E pollLast()
5962
System.out.println(ts.pollLast());
6063
System.out.println(ts);
6164
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import java.util.*;
2+
3+
class CricPlayer implements Comparable {
4+
5+
String name = null;
6+
CricPlayer(String name) {
7+
8+
this.name = name;
9+
}
10+
11+
public int compareTo(Object obj) {
12+
13+
return this.name.compareTo(((CricPlayer)obj).name);
14+
}
15+
16+
public String toString() {
17+
18+
return name;
19+
}
20+
}
21+
class TreeSetDemo {
22+
23+
public static void main(String[] args) {
24+
25+
TreeSet ts = new TreeSet();
26+
27+
ts.add(new CricPlayer("Rohit"));
28+
ts.add(new CricPlayer("Virat"));
29+
ts.add(new CricPlayer("MSD"));
30+
ts.add(new CricPlayer("MSD"));
31+
ts.add(new CricPlayer("Surya"));
32+
ts.add(new CricPlayer("Tilak"));
33+
ts.add(new CricPlayer("Tilak"));
34+
ts.add(new CricPlayer("Ishan"));
35+
36+
System.out.println(ts);
37+
}
38+
}

0 commit comments

Comments
 (0)