Commit 542a790
committed
feat(ComparatorVsComparableDemo): add demo comparing Comparable vs Comparator
What
- Added Student class implementing Comparable<Student>:
- Natural ordering defined: GPA descending, then Name ascending.
- compareTo(Student other) first compares GPA (higher first), then name.
- toString() overridden for readable output.
- Added ComparatorVsComparableDemo class with main method:
- Created list of Student objects (Charlie, Bob, Alice, Akshit).
- Demonstrated three sorting approaches:
1. Using Comparable (Collections.sort) → GPA desc, Name asc.
2. Using Comparator → sort by Name ascending.
3. Using Comparator with chaining → GPA ascending, then Name descending.
Why
- Demonstrates clear difference between Comparable and Comparator.
- Provides educational example with both natural ordering and flexible custom orderings.
- Shows practical usage of Comparator.comparing(), comparingDouble(), and thenComparing().
How
- Comparable:
- compareTo implemented in Student → defines single natural ordering.
- Used Collections.sort(students) with no comparator.
- Comparator:
- Used list.sort(Comparator.comparing(s -> s.name)) for name ascending.
- Used list.sort(Comparator.comparingDouble(s -> s.gpa).thenComparing(...)) for GPA asc, Name desc.
- Printed results after each sort to verify differences.
Logic
- Inputs: list of students with GPA and names.
- Outputs:
- After Comparable: [Akshit (3.9), Bob (3.7), Alice (3.5), Charlie (3.5)].
- After Comparator (Name asc): [Akshit, Alice, Bob, Charlie].
- After Comparator (GPA asc, Name desc): [Charlie (3.5), Alice (3.5), Bob (3.7), Akshit (3.9)].
- Flow:
1. Natural order sort via Comparable.
2. Custom order sort by name via Comparator.
3. Another custom sort by GPA asc, then name desc.
- Edge cases:
- Students with identical GPA and name → treated as equal by compareTo.
- Complexity / performance: O(n log n) sort.
- Concurrency / thread-safety: Not thread-safe; local list only.
- Error handling: Not needed here.
Real-life applications
- Comparable: best when there’s one canonical ordering (IDs, dates, scores).
- Comparator: flexible for multi-criteria sorting in UIs, reports, or user-specified orders.
- Useful in databases, ranking systems, and sorting utilities.
Notes
- Comparable provides a single natural order hardcoded into the class.
- Comparator provides external, pluggable strategies, allowing multiple sort orders without modifying the class.
- Comparator chaining (thenComparing) simplifies multi-field sorting.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent fff036f commit 542a790
File tree
1 file changed
+81
-0
lines changed- Section 25 Collections Frameworks/List Interface/ArrayList/Comparator/src/Package1
1 file changed
+81
-0
lines changedLines changed: 81 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 | + | |
0 commit comments