Commit fff036f
committed
feat(Student): add Student class with Comparable and StudentDemo sorting example
What
- Added Student class:
- Fields: name (String), gpa (double).
- Constructor, getters.
- equals() and hashCode() implemented via Objects for correct equality.
- toString() overridden for debugging.
- Comparable<Student> implemented to sort by GPA descending.
- Added StudentDemo class:
- Creates list of Student objects (Charlie, Bob, Alice, Akshit).
- Demonstrates two sorting approaches:
1. Manual lambda comparator (GPA descending, then name descending).
2. Comparator built using Comparator.comparing().reversed().thenComparing(Student::getName).
- Prints student list after each sort.
Why
- Illustrates multiple ways of defining natural ordering and custom comparators in Java.
- Provides educational example of sorting by GPA (descending) and tie-breaking by name.
- Demonstrates the relationship between Comparable and Comparator utilities.
How
- In Student:
- Implemented compareTo(Student o) → Double.compare(o.getGpa(), this.getGpa()).
- Ensures natural ordering is GPA descending.
- In StudentDemo:
- Created manual comparator lambda replicating GPA-first then name.
- Applied students.sort(lambda) → prints first ordering.
- Built Comparator via Comparator.comparing(Student::getGpa).reversed().thenComparing(Student::getName).
- Applied Collections.sort(students, comparator) → prints second ordering.
Logic
- Inputs: list of students with names and GPAs.
- Outputs: two printouts of student list after different sorts.
- Flow:
1. Construct student list.
2. Sort with manual lambda.
3. Print sorted list.
4. Sort with Comparator helper.
5. Print sorted list again.
- Example ordering:
- After both sorts, order is consistent: Akshit (3.9), Bob (3.7), Alice (3.5), Charlie (3.5).
- Edge cases:
- Students with same GPA and name → equals() ensures equality.
- GPA floating-point precision handled by Double.compare.
- Complexity / performance: O(n log n) sort.
- Concurrency / thread-safety: Not thread-safe, but list is local in main.
- Error handling: Not required.
Real-life applications
- Student ranking systems, leaderboards, or GPA sorting.
- Demonstrates how tie-breaking can be implemented in multiple styles.
- Shows best practice of combining Comparable (natural order) with explicit Comparator (custom order).
Notes
- Manual comparator uses subtraction logic; prefer Double.compare for clarity and to avoid edge-case bugs.
- Comparator.comparing() is more concise and readable.
- Using thenComparing() provides natural tie-breaking without nesting conditionals.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent e969dce commit fff036f
File tree
2 files changed
+104
-0
lines changed- Section 25 Collections Frameworks/List Interface/ArrayList/Comparator/src/Package
2 files changed
+104
-0
lines changedLines changed: 50 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 | + | |
Section 25 Collections Frameworks/List Interface/ArrayList/Comparator/src/Package/StudentDemo.java
Lines changed: 54 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 | + | |
0 commit comments