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