The Difference between Comparable and Comparator in Java

Keywords: Java

I. overview

Comparable and Comparator are used to compare and sort elements in a collection.
Comparable is a sort implemented by a method defined within a collection, located under java.lang.
Comparator is a sort implemented outside the collection, under java.util.

Comparable is an interface that the object itself supports the self-comparison. For example, String and Integer have implemented the Comparable interface themselves, which can complete the operation of comparison size. Custom classes can be sorted by adding list containers or by implementing Comparable interface. If you do not specify Comparator when sorting with Collections class, you can sort in natural order. The so-called natural order is to achieve the sort mode of the Comparable interface settings.

Comparator is a special comparator. When the object does not support self-comparison or the self-comparison function can not meet the requirements, a comparator can be written to complete the size comparison between two objects. Comparator embodies a strategy design pattern, that is, it does not change the object itself, but uses a strategy object to change its behavior.

In short, Comparable is a self-made comparison, Comparator is an external program to achieve comparison.

Demo Notes

Comparator

public class AbsComparator<T> implements Comparator<T>{

public int compare(Object o1, Object o2){
int ovalue1 = Math.abs(((Integer) o1).intValue());
int ovalue2 = Math.abs(((Integer) o2).intValue());
return (ovalue1 > ovalue2) ? 1 : (ovalue1 == ovalue2 ? 0 : -1);
    }
}




import java.util.Arrays;
import java.util.Random;

public class AbsComparatorTest{
public static void main(String[] args){
// Usage Method 1
Random rn = new Random();
Integer[] integerArray = new Integer[20];
for (int i = 0; i < integerArray.length; i++)
    {
    integerArray[i] = new Integer(rn.nextInt(100) * (rn.nextBoolean() ? 1 : -1));
    }
System.out.println("use Integer Built-in method sorting:");
Arrays.sort(integerArray);
System.out.println(Arrays.asList(integerArray));
System.out.println("use AbsComparator Sort:");
Arrays.sort(integerArray, new AbsComparator<Integer>());
System.out.println(Arrays.asList(integerArray));

// Usage Method 2
System.out.println("use AbsComparator compare-100 The absolute values of 10 and 10 are as follows:");
AbsComparator<Integer> absComparator = new AbsComparator<Integer>();
int result = absComparator.compare(new Integer(-100), new Integer(10));
System.out.println(result);
    }
}

Comparable

 class TestAge implements Comparable<Object>{

         private int age;

         public TestAge(int age){
             this.age=age;
         }
         @Override
         public int compareTo(Object o) {
              return this.age -((TestAge)o).age ;
         }

     }

Three, summary

It can be seen that Comparator is a comparator and Comparable is a comparison interface.
A class implements the Camparable interface, which indicates that the objects of this class can be compared with each other. The set of objects of this class can be sorted directly using the sort method.
Comparator can be seen as an implementation of an algorithm that separates the algorithm from the data. Comparator can also be used in the following two environments:
1. Designers of classes do not consider comparison issues and do not implement Comparable. They can sort by Comparator without changing the object itself.
2. Multiple sorting criteria can be used, such as ascending, descending, etc.

Posted by niesom on Sun, 31 Mar 2019 00:57:29 -0700