Skip to content

Commit 447d160

Browse files
committed
feat(ComparableInterfaceDemoIMP): add TreeSet demo with Point implementing Comparable
What - Added Point class: - Fields: x, y (int coordinates). - Constructor to initialize coordinates. - toString() overridden for readable output ("X=a Y=b"). - Implements Comparable: - compareTo(Object o): - Compares by x coordinate. - If equal, compares by y coordinate. - Returns negative/positive/0 accordingly. - Added ComparableInterfaceDemoIMP class: - Demonstrates storing Point objects in a TreeSet. - Inserts points (1,1), (5,5), (5,2). - Prints TreeSet, which maintains sorted order based on compareTo(). Why - Shows how TreeSet requires elements to be comparable. - Demonstrates defining natural ordering for custom objects. - Educational example: points ordered first by x, then by y. How - TreeSet<Point> created. - Added three points: - (1,1) → first. - (5,2) → before (5,5) since y=2 < 5 when x=5 equal. - TreeSet automatically sorts elements using compareTo(). - Printed TreeSet showing order. Logic - Inputs: points (1,1), (5,5), (5,2). - Outputs: sorted TreeSet: [X=1 Y=1, X=5 Y=2, X=5 Y=5]. - Flow: 1. TreeSet invokes compareTo() on insertion. 2. Orders by x ascending. 3. Break ties with y ascending. - Edge cases: - Equal (x,y) → compareTo returns 0 → treated as duplicate, not added. - Casting: compareTo(Object o) uses raw Comparable; better to implement Comparable<Point> for type safety. - Complexity / performance: - TreeSet operations O(log n) due to Red-Black tree. - Concurrency / thread-safety: - TreeSet is not thread-safe; local demo safe. - Error handling: - No explicit error handling; ClassCastException if mixed types inserted. Real-life applications - Managing sorted collections of coordinates, events, or objects with natural ordering. - Useful in computational geometry, scheduling, or leaderboard systems. - Demonstrates role of Comparable for user-defined types in sorted data structures. Notes - Comparable<Point> should be used instead of raw Comparable for generics safety. - TreeSet relies entirely on compareTo for both ordering and duplicate detection. - Comparator alternative could allow flexible custom orderings (e.g., sort by y first). Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent e0e870b commit 447d160

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package Package2;
2+
3+
import java.util.TreeSet;
4+
5+
// A simple Point class representing a 2D coordinate.
6+
// Implements Comparable so that Point objects can be sorted in a TreeSet.
7+
class Point implements Comparable {
8+
int x;
9+
int y;
10+
11+
// Constructor to initialize a Point with x and y coordinates.
12+
public Point(int x, int y) {
13+
this.x = x;
14+
this.y = y;
15+
}
16+
17+
// Provide a human-readable string representation of the Point.
18+
@Override
19+
public String toString() {
20+
return "X=" + x + " Y=" + y;
21+
}
22+
23+
/* compareTo() defines the "natural ordering" for Point objects.
24+
* - First compare by x-coordinate.
25+
* - If x is equal, then compare by y-coordinate.
26+
* - Returns:
27+
* - negative if this < other,
28+
* - positive if this > other,
29+
* - 0 if both coordinates are equal.
30+
*/
31+
32+
@Override
33+
public int compareTo(Object o) {
34+
Point p = (Point) o;
35+
36+
if (this.x < p.x)
37+
return -1;
38+
else if (this.x > p.x)
39+
return 1;
40+
else {
41+
if (this.y < p.y)
42+
return -1;
43+
else if (this.y > p.y)
44+
return 1;
45+
else
46+
return 0;
47+
}
48+
}
49+
}
50+
51+
public class ComparableInterfaceDemoIMP {
52+
public static void main(String[] args) {
53+
/* In this program, we are storing objects of our own class (Point) inside a TreeSet.
54+
* TreeSet:
55+
* - A TreeSet is a sorted set (implements NavigableSet).
56+
* - It requires elements to be comparable in order to maintain sorted order.
57+
58+
* Therefore:
59+
* - Our Point class implements Comparable so that TreeSet can compare Point objects.
60+
* - The compareTo() method defines how two Point objects are compared.
61+
*/
62+
63+
TreeSet<Point> TS = new TreeSet<>();
64+
65+
// Objects are compared using the compareTo() method defined in Point.
66+
TS.add(new Point(1, 1));
67+
TS.add(new Point(5, 5));
68+
TS.add(new Point(5, 2)); // Will be placed before (5,5) since y=2 < y=5
69+
70+
// Printing TreeSet will show sorted order of Points (by x, then by y).
71+
System.out.println(TS);
72+
}
73+
}
74+
75+
/* Summary:
76+
- This program demonstrates how to store objects of a user-defined class (Point) inside a TreeSet.
77+
- Since TreeSet is a sorted collection, objects must be comparable.
78+
- By implementing Comparable and overriding compareTo(), we define the natural ordering of Point objects.
79+
80+
1. Why Comparable?
81+
- TreeSet is a sorted set (backed by a Red-Black Tree).
82+
- It requires a way to compare objects to maintain order.
83+
- For user-defined classes, we must provide comparison logic.
84+
- Implementing Comparable and overriding compareTo() achieves this.
85+
86+
2. Ordering Logic in compareTo():
87+
- First compares by x-coordinate.
88+
- If x values are equal, then compare by y-coordinate.
89+
- Returns:
90+
- Negative → this < other
91+
- Positive → this > other
92+
- 0 → both points are equal
93+
94+
3. Output Example:
95+
Input points: (1,1), (5,5), (5,2)
96+
Sorted order: [(1,1), (5,2), (5,5)]
97+
98+
4. Key Notes:
99+
- TreeSet automatically sorts using compareTo().
100+
- Duplicate elements (same x and y) are not stored.
101+
- Comparable → natural ordering inside the class.
102+
- Comparator (alternative) → external custom ordering.
103+
*/

0 commit comments

Comments
 (0)