Detailed explanation and difference between Java Comparable and Comparator
Java provides us with two comparison mechanisms: Comparable and Comparator. What is the difference between them? Let’s find out today.
Comparable Natural Sort
Comparable is an interface under the java.lang package, and there is only one method that compareTo() inside:
public interface Comparable<T> { public int compareTo(T o);}Comparable allows objects of the class that implements it to be compared. The specific comparison rules are performed according to the rules in the compareTo method. This order is called natural order.
There are three cases where the return value of the compareTo method is:
Notice:
1. Since null is not a class or an object, you should pay attention to the case of e.compareTo(null) when rewriting the compareTo method. Even if e.equals(null) returns false, the compareTo method should actively throw a null pointer exception NullPointerException.
2. When implementing the Comparable class to rewrite the compareTo method, the result of e1.compareTo(e2) == 0 is generally required to be consistent with e1.equals(e2). In this way, in the future, when using SortedSet and other collection containers sorted according to the natural sort of classes, the order of saved data can be ensured to be consistent with the imagination.
Some people may be curious about what will happen if the second point above is violated?
For example, if you add two objects a and b to a SortedSet, ab satisfies (!a.equals(b) && a.compareTo(b) == 0), and there is no other Comparator specified, then when you add a and then b, it will add false, and the size of the SortedSet will not increase, because in SortedSet they are the same, and duplication is not allowed in SortedSet.
In fact, the results of all Java core classes that implement the Comparable interface are consistent with the equalas method.
List or arrays that implement the Comparable interface can be sorted using the Collections.sort() or Arrays.sort() methods.
Only objects that implement the Comparable interface can be used directly as keys of SortedMap (SortedSet), otherwise the Comparator sorting rules must be specified outside.
Therefore, if the class you define yourself wants to use ordered collection classes, you need to implement the Comparable interface, such as:
** * description: The entity class book used for testing, implements the Comparable interface, natural sorting* <br/> * author: shixinzhang * <br/> * data: 10/5/2016 */public class BookBean implements Serializable, Comparable { private String name; private int count; public BookBean(String name, int count) { this.name = name; this.count = count; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getCount() { return count; } public void setCount(int count) { this.count = count; } /** * Rewrite equals * @param o * @return */ @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof BookBean)) return false; BookBean bean = (BookBean) o; if (getCount() != bean.getCount()) return false; return getName().equals(bean.getName()); } /** * Rewrite the calculation method of hashCode* Iterative calculation based on all attributes to avoid duplication* The calculation factor 31 is seen when calculating hashCode. It is a prime number and cannot be divided again * @return */ @Override public int hashCode() { // Call String's hashCode(), which only represents the content of a string int result = getName().hashCode(); //Multiply by 31, plus count result = 31 * result + getCount(); return result; } @Override public String toString() { return "BookBean{" + "name='" + name + '/'' + ", count=" + count + '}'; } /** * When adding BookBean to TreeSet, this method will be called for sorting* @param another * @return */ @Override public int compareTo(Object another) { if (another instanceof BookBean){ BookBean anotherBook = (BookBean) another; int result; //For example, sorting by book price here result = getCount() - anotherBook.getCount(); //Or follow String The order of comparison //result = getName().compareTo(anotherBook.getName()); if (result == 0){ //When the book price is the same, compare the title. Make sure that all attributes are compared result = getName().compareTo(anotherBook.getName()); } return result; } // Return 0 return 0; }The above code also rewritten the equalas() and hashCode() methods. Custom classes need to rewritten these methods when they want to compare them.
When rewriting compareTo later, you need to judge whether a certain attribute is the same and compare all attributes once.
The Comparable interface is part of the Java Collections framework.
Comparator Custom Sort
Comparator is also an interface under the java.util package. Before JDK 1.8, there were only two methods:
public interface Comparator<T> { public int compare(T lhs, T rhs); public boolean equals(Object object);}Many new methods have been added to JDK 1.8:
Basically, they are all related to Function, and the new additions to 1.8 will not be introduced here.
From the above, we can see that using natural sorting requires classes to implement Comparable, and the comparaTo method is rewrited internally.
Comparator sets sorting rules externally and passes them to certain classes as sorting policy parameters, such as Collections.sort(), Arrays.sort(), or some internally ordered collections (such as SortedSet, SortedMap, etc.).
How to use it is mainly divided into three steps:
1. Create a Comparator interface implementation class and assign a value to an object
Write sorting rules for custom classes in the compare method
2. Pass the Comparator object as a parameter to a method of the sorting class
3. Add a custom class used in the compare method to the sorting class
For example:
// 1. Create an object that implements the Comparator interface Comparator = new Comparator() { @Override public int compare(Object object1, Object object2) { if (object1 instanceof NewBookBean && object2 instanceof NewBookBean){ NewBookBean newBookBean = (NewBookBean) object1; NewBookBean newBookBean1 = (NewBookBean) object2; //Refer to the comparisonTo in natural sorting for the specific comparison method Method, here is a chestnut return newBookBean.getCount() - newBookBean1.getCount(); } return 0; } }; //2. Pass this object as a formal parameter to the constructor of TreeSet TreeSet = new TreeSet(comparator); //3. Add the object of the class designed in the compare method in step 1 to the TreeSet to the TreeSet treeSet.add(new NewBookBean("A",34)); treeSet.add(new NewBookBean("S",1)); treeSet.add(new NewBookBean("V",46)); treeSet.add( new NewBookBean("Q",26));In fact, we can see that the use of Comparator is a strategy model. Students who are not familiar with the strategy model can click here to view: Strategy Mode: Learn about the fixed routine of online novels.
A reference to the Comparator interface is held in the sort class:
Comparator<? super K> comparator;
We can pass in various custom sorting rules Comparator implementation classes and formulate different sorting strategies for the same class.
Summarize
Two sorting methods in Java:
Comparable Natural sorting. (Entity class implementation)
Comparator is a custom sort. (If the entity class cannot be modified, it is created directly on the caller)
When it exists at the same time, it uses the rules of Comparator (custom sorting) for comparison.
For some ordinary data types (such as String, Integer, Double...), they implement the Comparable interface by default and implement the compareTo method, which we can use directly.
For some custom classes, they may need to implement different comparison strategies in different situations. We can create a new Comparator interface and then compare it using a specific Comparator implementation.
This is the difference between Comparable and Comparator.
Thank you for reading, I hope it can help you. Thank you for your support for this site!