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