Home  >  Java 8

Java Comparator.reversed

By Arvind Rai, March 07, 2019
reversed is the default method of Java Comparator functional interface. This method is introduced in Java 8. reversed returns a Comparator that imposes the reverse ordering of this Comparator. It has been declared as following.
default Comparator<T> reversed() 
To use reversed method, we need to instantiate our Comparator and call this method. reversed will return new instance of Comparator that will impose the reverse ordering of this comparator. Find the sample code to use this method.
Comparator<Student> nameComparator = (s1, s2) -> s1.getName().compareTo(s2.getName());
Collections.sort(list, nameComparator.reversed()); 
Comparator instance can be passed as an argument in Stream.sorted, List.sort and Collections.sort to sort the elements. Now lets us discuss using Comparator.reversed step-by-step.

Comparator.reversed with Stream.sorted

Stream.sorted returns a stream consisting of the elements of this stream, sorted according to the provided comparator.
StreamSortedDemo.java
package com.concretepage;
import java.util.Comparator;
import java.util.List;

public class StreamSortedDemo {
  public static void main(String[] args) {
	List<Student> list = Student.getStudentList();
	Comparator<Student> ageComparator = (s1, s2) -> s1.getAge() - s2.getAge();	
	list.stream().sorted(ageComparator.reversed()).forEach(s -> System.out.print(s.getAge() + " "));
	System.out.println("\n-----------");
	Comparator<Student> nameComparator = (s1, s2) -> s1.getName().compareTo(s2.getName());	
	list.stream().sorted(nameComparator.reversed()).forEach(s -> System.out.print(s.getName() + " "));
	System.out.println("\n-----------");	
	list.stream().sorted(Comparator.comparing(Student::getAge).reversed()).forEach(s -> System.out.print(s.getAge() + " "));
	System.out.println("\n-----------");
	list.stream().sorted(Comparator.comparing(Student::getName).reversed()).forEach(s -> System.out.print(s.getName() + " "));	
  }
}
Student.java
package com.concretepage;
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;
    }
} 
Output
22 21 20 19 18 
-----------
Shyam Ram Mohan Mahesh Krishna 
-----------
22 21 20 19 18 
-----------
Shyam Ram Mohan Mahesh Krishna 

Comparator.reversed with List.sort

List.sort sorts this list according to the given Comparator instance.
ListSortDemo.java
package com.concretepage;
import java.util.Comparator;
import java.util.List;

public class ListSortDemo {
  public static void main(String[] args) {
	List<Student> list = Student.getStudentList();
	Comparator<Student> ageComparator = (s1, s2) -> s1.getAge() - s2.getAge();	
	list.sort(ageComparator.reversed());
	list.forEach(s -> System.out.print(s.getAge() + " "));
	System.out.println("\n-----------");
	Comparator<Student> nameComparator = (s1, s2) -> s1.getName().compareTo(s2.getName());	
	list.sort(nameComparator.reversed());
	list.forEach(s -> System.out.print(s.getName() + " "));
	System.out.println("\n-----------");
	list.sort(Comparator.comparing(Student::getAge).reversed());	
	list.forEach(s -> System.out.print(s.getAge() + " "));	
	System.out.println("\n-----------");
	list.sort(Comparator.comparing(Student::getName).reversed());		
	list.forEach(s -> System.out.print(s.getName() + " ")); 	
  }
} 

Comparator.reversed with Collections.sort

Collections.sort sorts the specified list according to the given Comparator instance.
CollectionsSortDemo.java
package com.concretepage;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class CollectionsSortDemo {
  public static void main(String[] args) {
	List<Student> list = Student.getStudentList();
	Comparator<Student> ageComparator = (s1, s2) -> s1.getAge() - s2.getAge();	
	Collections.sort(list, ageComparator.reversed());
	list.forEach(s -> System.out.print(s.getAge() + " "));
	System.out.println("\n-----------");
	Comparator<Student> nameComparator = (s1, s2) -> s1.getName().compareTo(s2.getName());	
	Collections.sort(list, nameComparator.reversed());
	list.forEach(s -> System.out.print(s.getName() + " ")); 
	System.out.println("\n-----------");
	Collections.sort(list, Comparator.comparing(Student::getAge).reversed());	
	list.forEach(s -> System.out.print(s.getAge() + " "));	
	System.out.println("\n-----------");
	Collections.sort(list, Comparator.comparing(Student::getName).reversed());		
	list.forEach(s -> System.out.print(s.getName() + " ")); 	
  }
} 

References

Interface Comparator
Java 8 Stream sorted() Example
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us