Home  >  Java 8

Java 8 Comparator Changes for Lambda Expression Method Example

By Arvind Rai, November 29, 2014
Java 8 has done changes in java.util.Comparator to support Lambda Expression. New Methods has been added that will use Java 8 function to define a Comparator. In this page we are providing the example of new methods of Comparator added in Java 8. Find the Student class which we are using in our example for comparator.
Student.java
package com.concretepage.util;
import java.util.Arrays;
import java.util.List;
public class Student {
    private String name;
    private int age;
    public Student(String name,int age){
        this.name=name;
        this.age=age;
    }
    public String getName() {
        return name;
    }
    public int getAge() {
        return age;
    }
    public static List<Student> getStudentList(){
        Student s1 = new Student("Ram", 18);
        Student s2 = new Student("Shyam",22);
        Student s3 = new Student("Mohan",19);
        Student s4 = new Student("Mahesh",20);
        Student s5 = new Student("Krishna",21);
        List<Student> list = Arrays.asList(s1,s2,s3,s4,s5);
        return list;
    }
} 

Comparator.comparing and Comparator.reversed

Comparator.comparing creates a Comparator for the given lambda expression as Function. To reverse the Comparator we can use Comparator.reversed.
ComparatorComparing.java
package com.concretepage.util;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class ComparatorComparing {
    public static void main(String[] args) {
        List<Student> list = Student.getStudentList();
        Comparator<Student> ageComparator = Comparator.comparing(Student::getAge); 
        Collections.sort(list, ageComparator);
        list.forEach(s->System.out.println("Name:"+s.getName()+" Age:"+s.getAge()));
        //Using reversed order for sorting
        System.out.println("--Using reversed order for sorting--");
        Collections.sort(list, ageComparator.reversed());
        list.forEach(s->System.out.println("Name:"+s.getName()+" Age:"+s.getAge()));
    }
} 
Find the output.
Name:Ram Age:18
Name:Mohan Age:19
Name:Mahesh Age:20
Name:Krishna Age:21
Name:Shyam Age:22
--Using reversed order for sorting--
Name:Shyam Age:22
Name:Krishna Age:21
Name:Mahesh Age:20
Name:Mohan Age:19
Name:Ram Age:18 

Comparator.thenComparing

Comparator.thenComparing is a default method of the Comparator introduced in Java 8. If we have two Comparator instance, then we can use both comparator using Comparator.thenComparing. We call another Comparator by first Comparator. Find the example.
ThenComparing.java
package com.concretepage.util;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class ThenComparing {
    public static void main(String[] args) {
        Student s1 = new Student("Shyam", 22);
        Student s2 = new Student("Ram",22);
        Student s3 = new Student("Mohan",19);
        List<Student> list = Arrays.asList(s1,s2,s3);
        Comparator<Student> ageComparator = Comparator.comparing(Student::getAge); 
        Comparator<Student> nameComparator = Comparator.comparing(Student::getName); 
        Collections.sort(list, ageComparator.thenComparing(nameComparator));
        list.forEach(s->System.out.println("Name:"+s.getName()+" Age:"+s.getAge()));
    }
} 
Find the output.
Name:Mohan Age:19
Name:Ram Age:22
Name:Shyam Age:22 

Comparator.naturalOrder and Comparator.reverseOrder

Comparator.naturalOrder is used with the objects of the class which has implemented Comparable interface. We need do some changes in our Student class. Now Student class should implement Comparable interface and add below code snippet.
@Override
public int compareTo(Student o) {
     return this.name.compareTo(o.name);
} 
Use Comparator.naturalOrder that will short in natural order and to reverse it use Comparator.reverseOrder.
ComparatorNaturalOrder.java
package com.concretepage.util;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class ComparatorNaturalOrder {
    public static void main(String[] args) {
        List<Student> list = Student.getStudentList();
        Collections.sort(list, Comparator.naturalOrder());
        list.forEach(s->System.out.println("Name:"+s.getName()+" Age:"+s.getAge()));
        //Using natural reversed order for sorting
        System.out.println("--Using natural reversed order for sorting--");
        Collections.sort(list, Comparator.reverseOrder());
        list.forEach(s->System.out.println("Name:"+s.getName()+" Age:"+s.getAge()));
    }
} 
Find the output.
Name:Krishna Age:21
Name:Mahesh Age:20
Name:Mohan Age:19
Name:Ram Age:18
Name:Shyam Age:22
--Using natural reversed order for sorting--
Name:Shyam Age:22
Name:Ram Age:18
Name:Mohan Age:19
Name:Mahesh Age:20
Name:Krishna Age:21 
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS


©2019 concretepage.com | Privacy Policy | Contact Us