

They use the ordering defined by the Comparator interface’s compare() function to sort the objects. Objects class Employee implements Comparable ]Īs you saw in the above example, All the sorting methods also accept an instance of a Comparator interface. The Comparable interface has a single method called compareTo() that you need to implement in order to define how an object compares with the supplied object. To make an object comparable, the class must implement the Comparable interface. Java Comparable interface intuitionīy default, a user defined class is not comparable. Once you define how the objects should be compared using any of these interfaces, you’ll be able to sort them using various library functions like Collections.sort or Arrays.sort. For this purpose, Java provides two interfaces called Comparable and Comparator. You need to explicitly define how the objects of user defined classes should be compared. For example, how would you compare two Employees? how would you compare two Students? Comparing primitive values like int, char, float is very easy and can be done with comparison operators like, = etc.īut comparing objects is a little different. We often need to compare two values in our Java programs. It also reduces the probability of a introducing bug if we are to write it in pre-Java 8 style (imperative).Java Comparable and Comparator interface examples Rajeev Singh Java J3 mins read
COMPARETO METHOD MAP JAVA CODE
They help us write it in a declarative way which makes our code more readable. The static comparing methods in the Comparator interface helps us to write custom Comparator logic in order to compare or order two instances. thenComparingLong(Inventory::getInventoryId)) Summary The first example in the above section can be written using thenComparingLong as inventores.sort(paringInt((Inventory i) -> i.getItems().size()) Similar to comparingInt, comparingLong and comparingDouble that we use when working with primitives, there are thenComparing versions of them too ( thenComparingLong, thenComparingInt and thenComparingDouble). This method returns, zero if this object represents the same boolean value as the argument.
compareTo in interface Comparablepublic int compareTo(Boolean b) Specified by. inventories.sort(paringInt((Inventory i) -> i.getItems().size())Ĭparing(Location::getState))) Comparator thenComparingLong Following is the declaration for () method. If we have to use the Location name as the tie breaker, then we can plug in the Comparator we wrote earlier for the thenComparing. Public Inventory(Long inventoryId, Location location, Map items) Let us say we have the following data model representing an Inventory. If o1 is greater than o2, then a positive number must be returned.If o1 is lesser than o2, then a negative number must be returned. Since we store java objects in Collection there are also certain Set and Map which provides automating sorting when you insert element on that e.g.It takes two arguments and determines the ordering between them. It’s signature is int compare(T o1, T o2) ComparatorĪ Comparator is a FunctionalInterface that has a single abstract method compare. We will explore them and see how we can use them. From Java 8+, the Comparator has several static utility methods we can use (Comparator comparing methods). We can use it on data structures like SortedSet or a SortedMap to determine the order in which objects will be stored in such structures. We can use a Comparator to sort a list of objects. The Comparator interface is a comparison function that imposes a total ordering on a collection of objects. Comparator comparing – Passing a Comparator.
