Skip to content

Commit fff036f

Browse files
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

2 files changed

+104
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package Package;
2+
3+
import java.util.Objects;
4+
5+
/**
6+
* Student data class.
7+
* Comparable implementation sorts by GPA descending (higher GPA first).
8+
*/
9+
10+
public class Student implements Comparable<Student> {
11+
private String name;
12+
private double gpa;
13+
14+
public Student(String name, double gpa) {
15+
this.name = name;
16+
this.gpa = gpa;
17+
}
18+
19+
public String getName() { return name; }
20+
public double getGpa() { return gpa; }
21+
22+
@Override
23+
public boolean equals(Object o) {
24+
if (this == o) return true;
25+
if (o == null || getClass() != o.getClass()) return false;
26+
Student student = (Student) o;
27+
return Double.compare(gpa, student.gpa) == 0 && Objects.equals(name, student.name);
28+
}
29+
30+
@Override
31+
public int hashCode() {
32+
return Objects.hash(name, gpa);
33+
}
34+
35+
@Override
36+
public String toString() {
37+
return "Student{" +
38+
"name='" + name + '\'' +
39+
", gpa=" + gpa +
40+
'}';
41+
}
42+
43+
/**
44+
* Comparable: sorts by GPA descending (higher GPA -> earlier).
45+
*/
46+
@Override
47+
public int compareTo(Student o) {
48+
return Double.compare(o.getGpa(), this.getGpa());
49+
}
50+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package Package;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.Comparator;
6+
import java.util.List;
7+
8+
/**
9+
* Demonstrates sorting a List<Student>.
10+
* Keeps your original manual comparator logic intact and then
11+
* shows sorting with a Comparator built via Comparator.comparing().
12+
*/
13+
14+
public class StudentDemo {
15+
public static void main(String[] args) {
16+
List<Student> students = new ArrayList<>();
17+
students.add(new Student("Charlie", 3.5));
18+
students.add(new Student("Bob", 3.7));
19+
students.add(new Student("Alice", 3.5));
20+
students.add(new Student("Akshit", 3.9));
21+
22+
// Comparator constructed with Comparator helpers (keeps the same ordering intent)
23+
Comparator<Student> comparator = Comparator
24+
.comparing(Student::getGpa)
25+
.reversed()
26+
.thenComparing(Student::getName);
27+
28+
// The original manual sort using lambda you provided (kept exactly)
29+
students.sort((o1, o2) -> {
30+
if (o2.getGpa() - o1.getGpa() > 0) {
31+
return 1;
32+
} else if (o2.getGpa() - o1.getGpa() < 0) {
33+
return -1;
34+
} else {
35+
return o2.getName().compareTo(o1.getName());
36+
}
37+
});
38+
39+
// Print after the first sort
40+
System.out.println("After manual sort (original logic):");
41+
for (Student s : students) {
42+
System.out.println(s.getName() + ": " + s.getGpa());
43+
}
44+
45+
// Now sort again using the Comparator (same intended order)
46+
Collections.sort(students, comparator);
47+
48+
// Print after Collections.sort (comparator)
49+
System.out.println("\nAfter Collections.sort with comparator:");
50+
for (Student s : students) {
51+
System.out.println(s.getName() + ": " + s.getGpa());
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)