Skip to content

Instantly share code, notes, and snippets.

@adityachaudhari
Created October 3, 2021 07:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adityachaudhari/6158c8ee7c26ae2848e1a54e702cf699 to your computer and use it in GitHub Desktop.
Save adityachaudhari/6158c8ee7c26ae2848e1a54e702cf699 to your computer and use it in GitHub Desktop.
ComparatorBeforeAndWithJava8
package com.java8.comparator;
import com.java8.businessdataobjects.Student;
import java.util.*;
import java.util.stream.Collectors;
public class ComparatorBeforeAndWithJava8 {
public static void main(String args[]) {
List<Student> studentList = Arrays.asList(new Student(10, "Ram D", 20),
new Student(18, "Zeck C", 19),
new Student(11, "Tom P", 23),
new Student(9, "Ronny D", 19),
new Student(13, "John P", 20));
System.out.println("************ Sorting by name using traditional java without anonymous and java 8 ************ \n");
System.out.println("Before sorting by name \n"+studentList);
Collections.sort(studentList, new StudentNameComparator());
System.out.println("After sorting by name traditional way \n"+studentList);
System.out.println("\n************ Sorting age with anonymouys inner class ************");
System.out.println("Before sorting by age :"+studentList);
Collections.sort(studentList, new Comparator<Student>() {
@Override public int compare(Student student1, Student student2) {
return student1.getAge().compareTo(student2.getAge());
}
});
System.out.println("After sorting by age :"+studentList);
// comment Collections.sort() code above to check below code .
System.out.println("\n************ Sorting roll number with java 8 comparator ************ \n");
System.out.println("Before sorting roll no with java 8 lambda "+studentList);
// comparator to compare by roll no, name and age
Comparator<Student> compareByRollNumber = (Student stu1, Student stu2) -> stu1.getRollno().compareTo(stu2.getRollno());
Comparator<Student> compareByAge = (Student stu1, Student stu2) -> stu1.getAge().compareTo(stu2.getAge());
Comparator<Student> compareByName = (Student stu1, Student stu2) -> stu1.getName().compareTo(stu2.getName());
//approach 1 using Collections.sort
//Collections.sort(studentList, compareByRollNumber);
//System.out.println("After sorting roll no with lambda"+studentList);
//approach 2 ASC
List<Student> sortedByRollNoUsingStreams = studentList.stream()
.sorted(compareByRollNumber)
.collect(Collectors.toList());
System.out.println("After sorting roll no with lambda ASC order"+sortedByRollNoUsingStreams);
//approach 2 descending
List<Student> sortedByRollNoUsingStreamsDesc = studentList.stream()
.sorted(compareByRollNumber.reversed())
.collect(Collectors.toList());
System.out.println("After sorting roll no with lambda DESC order"+sortedByRollNoUsingStreamsDesc);
System.out.println("\n************ chain of sorts java 8 comparator ************ ");
System.out.println("Before comparing by age and then name"+studentList);
List<Student> comparingByAgeThenName = studentList.stream()
.sorted(compareByAge.thenComparing(compareByName))
.collect(Collectors.toList());
System.out.println("After comparing by age and then name"+comparingByAgeThenName);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment