Java Comparator interface

Java Comparator interface is used to order the objects of a user-defined class.

This interface is found in java.util package and contains 2 methods compare(Object obj1,Object obj2) and equals(Object element).

It provides multiple sorting sequences, i.e., you can sort the elements on the basis of any data member, for example, rollno, name, age or anything else.

Methods of Java Comparator Interface

Method Description
public int compare(Object obj1, Object obj2) It compares the first object with the second object.
public boolean equals(Object obj) It is used to compare the current object with the specified object.
public boolean equals(Object obj) It is used to compare the current object with the specified object.

Collections class

Collections class provides static methods for sorting the elements of a collection. If collection elements are of Set or Map, we can use TreeSet or TreeMap. However, we cannot sort the elements of List. Collections class provides methods for sorting the elements of List type elements also.

Method of Collections class for sorting List elements

public void sort(List list, Comparator c): is used to sort the elements of List by the given Comparator.


Java Comparator Example (Non-generic Old Style)

Let's see the example of sorting the elements of List on the basis of age and name. In this example, we have created 4 java classes:

  1. Student.java
  2. AgeComparator.java
  3. NameComparator.java
  4. Simple.java
Student.java

This class contains three fields rollno, name and age and a parameterized constructor.

AgeComparator.java

This class defines comparison logic based on the age. If the age of the first object is greater than the second, we are returning a positive value. It can be anyone such as 1, 2, 10. If the age of the first object is less than the second object, we are returning a negative value, it can be any negative value, and if the age of both objects is equal, we are returning 0.

NameComparator.java

This class provides comparison logic based on the name. In such case, we are using the compareTo() method of String class, which internally provides the comparison logic.

Simple.java

In this class, we are printing the values of the object by sorting on the basis of name and age.

       Sorting by Name
       106 Ajay 27
       105 Jai 21
       101 Vijay 23
       
       Sorting by age       
       105 Jai 21
       101 Vijay 23
       106 Ajay 27

Java Comparator Example (Generic)

Student.java
AgeComparator.java
NameComparator.java

This class provides comparison logic based on the name. In such case, we are using the compareTo() method of String class, which internally provides the comparison logic.

Simple.java

In this class, we are printing the values of the object by sorting on the basis of name and age.

       Sorting by Name
       106 Ajay 27
       105 Jai 21
       101 Vijay 23

       Sorting by age     
       105 Jai 21
       101 Vijay 23
       106 Ajay 27

Java 8 Comparator interface

Java 8 Comparator interface is a functional interface that contains only one abstract method. Now, we can use the Comparator interface as the assignment target for a lambda expression or method reference.

Methods of Java 8 Comparator Interface

Method Description
int compare(T o1, T o2) It compares the first object with second object.
static > Comparator comparing(Function keyExtractor) It accepts a function that extracts a Comparable sort key from a type T, and returns a Comparator that compares by that sort key.
static Comparator comparing(Function keyExtractor, Comparator keyComparator) It accepts a function that extracts a sort key from a type T, and returns a Comparator that compares by that sort key using the specified Comparator.
static Comparator comparingDouble(ToDoubleFunction keyExtractor) It accepts a function that extracts a double sort key from a type T, and returns a Comparator that compares by that sort key.
static Comparator comparingInt(ToIntFunction keyExtractor) It accepts a function that extracts an int sort key from a type T, and returns a Comparator that compares by that sort key.
static Comparator comparingLong(ToLongFunction keyExtractor) It accepts a function that extracts a long sort key from a type T, and returns a Comparator that compares by that sort key.
boolean equals(Object obj) It is used to compare the current object with the specified object.
static > Comparator naturalOrder() It returns a comparator that compares Comparable objects in natural order.
static Comparator nullsFirst(Comparator comparator) It returns a comparator that treats null to be less than non-null elements.
static Comparator nullsLast(Comparator comparator) It returns a comparator that treats null to be greater than non-null elements.
default Comparator reversed() It returns comparator that contains reverse ordering of the provided comparator.
static > Comparator reverseOrder() It returns comparator that contains reverse of natural ordering.
default Comparator thenComparing(Comparator other) It returns a lexicographic-order comparator with another comparator.
default > Comparator thenComparing(Function keyExtractor) It returns a lexicographic-order comparator with a function that extracts a Comparable sort key.
default Comparator thenComparing(Function keyExtractor, Comparator keyComparator) It returns a lexicographic-order comparator with a function that extracts a key to be compared with the given Comparator.
default Comparator thenComparingDouble(ToDoubleFunction keyExtractor) It returns a lexicographic-order comparator with a function that extracts a double sort key.
default Comparator thenComparingInt(ToIntFunction keyExtractor) It returns a lexicographic-order comparator with a function that extracts a int sort key.
default Comparator thenComparingLong(ToLongFunction keyExtractor) It returns a lexicographic-order comparator with a function that extracts a long sort key.

Java 8 Comparator Example

Let's see the example of sorting the elements of List on the basis of age and name.

File: Student.java

File: TestSort1.java

Sorting by Name
106 Ajay 27
105 Jai 21
101 Vijay 23
Sorting by Age
105 Jai 21
101 Vijay 23
106 Ajay 27

Java 8 Comparator Example: nullsFirst() and nullsLast() method

Here, we sort the list of elements that also contains null.

File: Student.java

File: TestSort2.java

Considers null to be less than non-null
105 null 21
106 Ajay 27
101 Vijay 23
Considers null to be greater than non-null
106 Ajay 27
101 Vijay 23
105 null 21




Contact US

Email:[email protected]

Comparator interface
10/30