Commit cee6a29
committed
feat(Demo): add Student2 comparator demo with GPA ascending, GPA descending, and GPA+Name sorting
What
- Added Student2 class:
- Fields: name (String), gpa (double).
- Constructor to initialize fields.
- Getters for name and gpa.
- Added Demo class:
- Creates a list of Student2 objects: Alice (3.5), Bob (3.1), Charlie (3.9), Akshit (3.4), Smith (3.4).
- Demonstrates multiple Comparator usages:
1. Comparator<Student2> comparator = Comparator.comparing(Student2::getGpa) → sorts by GPA ascending.
2. Comparator<Student2> comparator2 = Comparator.comparing(Student2::getGpa).reversed() → sorts by GPA descending.
3. Comparator<Student2> comparator3 = Comparator.comparing(Student2::getGpa).reversed().thenComparing(Student2::getName) → GPA descending, then Name ascending.
- Prints list after each sort.
Why
- Illustrates modern Comparator API (Java 8+) using method references and chaining.
- Shows flexibility of Comparator: same data can be sorted in multiple ways without modifying the class.
- Demonstrates reversed() and thenComparing() for secondary ordering.
How
- students.sort(comparator) → ascending GPA.
- students.sort(comparator2) → descending GPA.
- students.sort(comparator3) → descending GPA, then ascending Name.
- Printed results after each sorting phase.
Logic
- Inputs: list of 5 students with names and GPAs.
- Outputs:
1. GPA ascending: [Bob (3.1), Akshit (3.4), Smith (3.4), Alice (3.5), Charlie (3.9)].
2. GPA descending: [Charlie (3.9), Alice (3.5), Akshit (3.4), Smith (3.4), Bob (3.1)].
3. GPA descending + Name ascending:
- [Charlie (3.9), Alice (3.5), Akshit (3.4), Smith (3.4), Bob (3.1)].
- Tie between Akshit and Smith at 3.4 resolved by name order.
- Flow:
1. Define comparator(s).
2. Sort with comparator(s).
3. Print list after each.
- Edge cases:
- Ties resolved by thenComparing(Student2::getName).
- Equal GPA and equal name → treated as equal by comparator.
- Complexity / performance: O(n log n).
- Concurrency / thread-safety: Local list; not thread-safe for concurrent modification.
- Error handling: Not required.
Real-life applications
- Ranking students by GPA or GPA + Name in academic systems.
- Sorting employees by salary then name.
- Common in leaderboard or reporting tools where multi-level sorting is required.
Notes
- Comparator.comparingDouble(Student2::getGpa) can be used instead of comparing for primitives to avoid boxing.
- Comparator.comparing(Student2::getName, String.CASE_INSENSITIVE_ORDER) could handle case-insensitive name sorting.
- Demonstrates fluent API style of Comparator chaining introduced in Java 8.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent d971cdd commit cee6a29
File tree
1 file changed
+12
-14
lines changed- Section 25 Collections Frameworks/List Interface/ArrayList/Comparator/src
1 file changed
+12
-14
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | 1 | | |
4 | 2 | | |
5 | 3 | | |
| |||
9 | 7 | | |
10 | 8 | | |
11 | 9 | | |
12 | | - | |
13 | | - | |
| 10 | + | |
14 | 11 | | |
15 | 12 | | |
16 | 13 | | |
| |||
24 | 21 | | |
25 | 22 | | |
26 | 23 | | |
27 | | - | |
| 24 | + | |
28 | 25 | | |
29 | 26 | | |
30 | 27 | | |
| |||
46 | 43 | | |
47 | 44 | | |
48 | 45 | | |
49 | | - | |
50 | | - | |
| 46 | + | |
51 | 47 | | |
52 | 48 | | |
53 | 49 | | |
| |||
56 | 52 | | |
57 | 53 | | |
58 | 54 | | |
59 | | - | |
60 | | - | |
| 55 | + | |
61 | 56 | | |
62 | 57 | | |
63 | 58 | | |
64 | | - | |
65 | | - | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
66 | 65 | | |
67 | 66 | | |
68 | | - | |
69 | | - | |
| 67 | + | |
70 | 68 | | |
71 | 69 | | |
72 | 70 | | |
73 | | - | |
| 71 | + | |
0 commit comments