The Comparator interface is a part of the java.util package and apart from the compare method; it also contains another method named equals. The Comparator interface contains a method called compare() that you need to implement in order to define the ordering of the objects of a class - public interface Comparator { int compare(T o1, T o2); } The implementation of the compare() method should return. Using Comparable Interface. Java Comparator Interface Definition. It returns the result in integer equivalent value by comparing the two int method arguments.The value returned is … Note that Comparator is a functional interface and int compare(T o1, T o2) is the only abstract method. The Java Comparator interface is located in the java.util package. public int compare (Object obj1, Object obj2); The compare method compares obj1 with obj2. Methods of Java 8 Comparator Interface a negative integer, if the first argument is less than the second, It is used to compare two objects for sorting/ordering. The Java Comparator interface definition looks like this: public interface Comparator { public int compare(T o1, T o2); } Notice that the Java Comparator interface only has a single method. Java 8 Comparator interface is a functional interface that contains only one abstract method. Since -1 is much less than the Integer.MAX_VALUE, “Roger” should come before the “John” in the sorted collection. Comparator interface in Java is used to define the order of objects of the custom or user defined class. Java Integer compare() method. This tutorial helps you how to use the Arrays utility class to sort elements in an array.. You know, the java.util.Arrays class provides various methods for sorting elements of an array, as simple as:. In order to compare the set of elements in an object , The comparator provides the multiple sorting sequences. The Java Comparator interface is contained in the java.util package, so you need to import java.util package in order to use the Comparator interface in the code. Utilizing configurable strategies, Java Comparator can contrast objects with profit a number based for a positive, equivalent or negative correlation. The class itself must implements the java.lang.Comparable interface to compare its instances. So, based on the Comparator/Comparable contract, the Integer.MAX_VALUE is less than -1, which is obviously incorrect. Let’s implement a Comparator example in Java. int compare(T o1, T o2) This method returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second. Now, we can use the Comparator interface as the assignment target for a lambda expression or method reference. A comparison function, which imposes a total ordering on some collection of objects. Summoned by “java.util.comparator,” Java Comparator analyzes two Java protests in a “compare(Object 01, Object 02)” group. Java Comparator is an interface for arranging Java objects. Java provides two interfaces to sort objects using data members of the class: Comparable; Comparator . Arrays.sort(array) This tutorial shows various examples of sorting an array using such methods, especially using the Comparable and Comparator interfaces. Java Comparator Example. A comparable object is capable of comparing itself with another object. Hence we can sort the object based on any property. However, due to integer overflow, the “Integer.MAX_VALUE – (-1)” will be less than zero. The compare() method is a method of Integer class under java.lang package.This method compares two integer values numerically. The Comparator interface contains two methods, compare and equals. Comparator in Java is an interface which is coming from java.util package, used to ordering the collection of objects. Java 8 Comparator interface.