Hey there!
Welcome to ClearUrDoubt.com.
In this post, we will look at a Program to sort objects using Comparator Interface in Java 8.
Consider a Student object having properties like rollno, name and marks. We will try to sort the objects based on rollno and name.
Below is the Student class definition:
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 |
package com.sample.clearurdoubt.compare; public class Student { int rollno; String name; int marks; public Student(int rollno, String name, int marks) { this.rollno = rollno; this.name = name; this.marks = marks; } public int getRollno() { return rollno; } public void setRollno(int rollno) { this.rollno = rollno; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getMarks() { return marks; } public void setMarks(int marks) { this.marks = marks; } } |
Here is the Java program to sort the Students collection using Comparator class:
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 |
package com.sample.clearurdoubt.compare; import java.util.List; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; public class StudentComparision { public static void main(String[] args) { //create student objects Student s1 = new Student(101, "Rahul", 560); Student s2 = new Student(102, "George", 523); Student s3 = new Student(101, "Abhay", 516); Student s4 = new Student(103, "David", 550); List<Student> students = new ArrayList<Student>(); students.add(s1); students.add(s2); students.add(s3); students.add(s4); System.out.println("=== Before Sorting ==="); for(Student s : students) { System.out.println(s.getRollno() + " :: " + s.getName() + " :: " + s.getMarks()); } System.out.println(); // Java 8 Comparator.comparing version Collections.sort(students, Comparator.comparing((Student s) -> s.getMarks()) .thenComparing(s -> s.getName())); // // ========================= This can be achieved by creating a new Comparator instance ============================= // class StudentComparator implements Comparator<Student> // { // @override // public int compare(Student s1, Student s2) // { // int i = s1.getRollno() - s2.getRollno(); // if(i != 0) // { // return i; // } // // i = s1.getName().compareTo(s2.getName()); // // return i; // } // } // // // sort students based on the StudentComparator // students.sort(new StudentComparator()); // // ================================================================================================================= System.out.println("=== After Sorting ==="); for(Student s : students) { System.out.println(s.getRollno() + " :: " + s.getName() + " :: " + s.getMarks()); } System.out.println(); // Java 8 Comparator.comparing version Collections.sort(students, Comparator.comparing((Student s) -> s.getRollno()) .thenComparing(s -> s.getName()).reversed()); System.out.println("=== After Reverse Sorting ==="); for(Student s : students) { System.out.println(s.getRollno() + " :: " + s.getName() + " :: " + s.getMarks()); } } } |
Output:
Happy Learning :).
Please leave a reply in case of any queries.