Commit 447d160
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- Section 25 Collections Frameworks/List Interface/ArrayList/Comparator/src/Package2
1 file changed
+103
-0
lines changedLines changed: 103 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 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 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
0 commit comments