Commit e0e870b
committed
feat(ComparableDemo): add demo of natural ordering with Comparable
What
- Added Student class in package Package2 implementing Comparable<Student>:
- Fields: name, gpa.
- Constructor to initialize fields.
- compareTo(Student other):
- Primary ordering: GPA descending (higher first).
- Secondary ordering: name ascending (alphabetical).
- toString() overridden for readable output.
- Added ComparableDemo class:
- Creates list of Student objects.
- Sorts list using natural ordering (list.sort(null)).
- Prints sorted students.
- Creates list of Integer objects.
- Sorts integers using their natural ordering (ascending).
- Prints sorted numbers.
Why
- Demonstrates how Comparable defines a natural order for objects.
- Shows how Collections can use natural ordering when sorting with null comparator.
- Provides educational comparison between Student (custom natural order) and Integer (built-in natural order).
How
- Student implements Comparable and defines compareTo() logic.
- In main():
- list.sort(null) sorts by Student’s compareTo() implementation.
- numbers.sort(null) sorts by Integer’s natural order.
- Printed results for both lists.
Logic
- Inputs:
- Students: Charlie (3.5), Bob (3.7), Alice (3.5), Akshit (3.9).
- Integers: [2, 1, 99].
- Outputs:
- Students sorted by GPA descending, then name ascending:
[Student{name='Akshit', gpa=3.9}, Student{name='Bob', gpa=3.7}, Student{name='Alice', gpa=3.5}, Student{name='Charlie', gpa=3.5}]
- Integers sorted ascending:
[1, 2, 99].
- Flow:
1. Define natural ordering in Student via compareTo.
2. Use sort(null) to rely on that natural ordering.
3. Print ordered collections.
- Edge cases:
- Equal GPA and equal name → considered equal by compareTo.
- Complexity / performance: O(n log n) sorting.
- Concurrency / thread-safety: Local lists; not thread-safe but safe in demo context.
- Error handling: Not applicable.
Real-life applications
- Ranking students by GPA with consistent tiebreakers.
- Comparable makes classes naturally sortable without requiring external comparators.
- Useful in data structures like TreeSet or TreeMap that rely on natural ordering.
Notes
- Passing null to sort() explicitly signals to use natural ordering.
- Comparator can still be supplied later for alternate orderings without modifying class.
- Good practice: ensure compareTo is consistent with equals() for predictable behavior in sorted collections.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 542a790 commit e0e870b
File tree
1 file changed
+78
-0
lines changed- Section 25 Collections Frameworks/List Interface/ArrayList/Comparator/src/Package2
1 file changed
+78
-0
lines changedLines changed: 78 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 | + | |
0 commit comments