Collections class provides static methods for sorting the elements of a collection. However, we cannot sort the elements of List. Collections class provides methods for sorting the elements of List type elements also.
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:. 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, 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.
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. 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.
JavaTpoint offers too many high quality services. Mail us on [email protected] , to get more information about given services. Please mail your requirement at [email protected] Duration: 1 week to 2 week.
Considers null to be less than non-null null 21 Ajay 27 Vijay 23 Considers null to be greater than non-null Ajay 27 Vijay 23 null Reinforcement Learning. R Programming. Hence output is 0. Compare enables you to order objects.
To do this, you have to create a class that implements comparator interface. In the above syntax, obj1 and obj2 are two objects that you have to compare using compare method. You have to ensure that the relation is transitive. In the below program of Java comparator example, there are 6 variables.
Equals verifies whether the number object is equal to the object, which is passed as an argument or not. This method takes two parameters 1 any object 2 return value. It returns true if the passed argument is not null and is an object of the similar type having the same numeric value. In the below example, we are comparing the four variables with each other. Three variables are of type integer and one of type short. The first result is false as the value of p, which is 5, is not equal to the value of q, which is The second result is true because the value of variable p and r is the same, which is 5.
Lastly, the output is false, as variable p and s have different assigned values. Skip to content. What is Comparable in Java? What is Comparator in Java? Comparable provides compareTo method to sort elements in Java whereas Comparator provides compare method to sort elements in Java.
Comparable interface is present in java. OR 2 What is the difference between Comparable and Comparator in java? OR 3 Which is preferred Comparable or Comparator? OR 4 Why do you need Comparable if Comparator can also sort things? You have to decide whether to use Comparable or to use Comparator. This article outline some of the facts which help you decide in which situations one should use Comparable and in which Comparator.
If you dont have access to the code of that class say class belongs to third party , then there is no choice but to use Comparator because Comparator does not need to change the original class. If your class implements Comparable then Arrays. You do not need to write separate comparators and pass them to overloaded sort as shown here.
Otherwise you have to write separate Comparator and pass it in the constructor of TreeMap. But this has a advantage also. You can add as many sorting criteria later as you want or modify the existing ones without changing the class whose instances you are sorting.
Thus comparators provides flexibilty while Comparable avoids extra classes. Note that you can also write Comparators as anonymous classes. In that case you can avoid separate comparators also. Though we can also compare objects of different types while using Comparable as shown here but we should avoid it. If you are going to compare instances of different classes then you should use Comparator.
But this was valid upto pre Java 5, before the introduction of generics. If their types would be different as: compare Integer o1, String o2 then this will give compile time error. Therefore if generic form of compare is used then it compares objects of only same types.
With non generic form of compare you can still compare objects of different types.
0コメント