Skip to content

Commit cee6a29

Browse files
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
Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
package ListDemo.ArrayListLinkedListStack;
2-
31
import java.util.ArrayList;
42
import java.util.Comparator;
53
import java.util.List;
@@ -9,8 +7,7 @@ class Student2 {
97
private double gpa;
108

119
// Constructor to initialize a Student object with name and GPA
12-
public Student2(String name, double gpa)
13-
{
10+
public Student2(String name, double gpa) {
1411
this.name = name;
1512
this.gpa = gpa;
1613
}
@@ -24,7 +21,7 @@ public String getName() {
2421
}
2522
}
2623

27-
public class ComparatorDemo2 {
24+
public class Demo {
2825
public static void main(String[] args) {
2926
// Create a list of Student2 objects (changed generic type from Student to Student2)
3027
List<Student2> students = new ArrayList<>();
@@ -46,8 +43,7 @@ public static void main(String[] args) {
4643

4744
// Printing the sorted list of students (by ascending GPA)
4845
System.out.println("Printing the sorted list of students (by ascending GPA)\n");
49-
for (Student2 student : students)
50-
{
46+
for (Student2 student : students) {
5147
System.out.println(student.getName() + ": " + student.getGpa());
5248
}
5349

@@ -56,18 +52,20 @@ public static void main(String[] args) {
5652
students.sort(comparator2);
5753

5854
System.out.println("\nreverse method add keya hai.");
59-
for (Student2 student : students)
60-
{
55+
for (Student2 student : students) {
6156
System.out.println(student.getName() + ": " + student.getGpa());
6257
}
6358

64-
//THis is using sort by their name.
65-
Comparator<Student2> comparator3 = Comparator.comparing(Student2::getGpa).reversed().thenComparing(Student2::getName);
59+
//This is using sort by their name.
60+
Comparator<Student2> comparator3 =
61+
Comparator.
62+
comparing(Student2::getGpa).
63+
reversed().
64+
thenComparing(Student2::getName);
6665

6766
System.out.println("\nTHis is using sort by their name.");
68-
for (Student2 student : students)
69-
{
67+
for (Student2 student : students) {
7068
System.out.println(student.getName() + ": " + student.getGpa());
7169
}
7270
}
73-
}
71+
}

0 commit comments

Comments
 (0)