-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComprableInterface.java
More file actions
50 lines (45 loc) · 1.08 KB
/
ComprableInterface.java
File metadata and controls
50 lines (45 loc) · 1.08 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
48
49
50
package Framework;
import java.util.TreeSet;
class Point implements Comparable{
int x;
int y;
Point(int x, int y){
this.x = x;
this.y = y;
}
@Override
public String toString() {
return "Point{" +
"x=" + x +
", y=" + y +
'}';
}
@Override
public int compareTo(Object o) {
Point p = (Point)o;
if(this.x < p.x)
return -1;
else if(this.x > p.x)
return 1;
else {
if (this.y > p.y)
return 1;
}
return -1;
}
}
public class ComparableInterface {
public static void main(String[] args) {
TreeSet<Point> P1 = new TreeSet<>();
P1.add(new Point(5,2));
P1.add(new Point(4,3));
P1.add(new Point(0,0));
P1.add(new Point(1,2));
P1.add(new Point(2,3));
P1.add(new Point(2,4));
// System.out.println(P1);
P1.pollLast(); // (5,2) deleted
P1.pollFirst(); // (0,0) deleted
P1.forEach((x)-> System.out.println(x));
}
}