Comparable, Comparator, and comparator

Source: Internet
Author: User

Comparable, Comparator, and comparator

Jdk1.8.0 _ 41

I know how to write code and how to sort the set. Just copy the keyboard and beat it at random.

Both Comparable and Comparator are used for sorting sets. For most people, Comparator may be a little more familiar than Comparable. The usage frequency of code similar to the following statements should be the highest.

Collections.sort(list, new Comparator<String>() {    @Override    public int compare(String o1, String o2) {        return o1.compareTo(o2);    }});

This is a piece of code for sorting sets.

The usage of the comparator in set sorting is often stopped here, so that the deeper usage is understandable and the more complex sorting is at a loss.

Comparable is used to sort the methods defined inside the set, and Comparator is used to sort the external implementations of the set.

We start with the two overload methods of Collections. sort.

Public static <T extends Comparable <? Super T> void sort (List <T> list)

Public static <T> void sort (List <T> list, Comparator <? Super T> c)

Both methods are generic methods. The first method only transmits a List parameter for sorting, and the second method transmits a List parameter with a Comparator.

 

Public static <T extends Comparable <? Super T> void sort (List <T> list)

The Collections. sort method is used to sort the List set and think about a problem. How to sort the List with only one parameter? What sort rules does it follow? The answer is "<T extends Comparable <? Super T> ", the elements in the List set need to implement the Comparable interface, and the Comparable interface is also a generic, the generic type must be the superclass (or itself) of the elements in the set ). The key point is that the elements in the set must implement the Comparable interface. That is to say, when you use the Collections. sort (List) method to sort the elements in the set, you need to implement the Comparable interface for the elements in the set.

 

Public static <T> void sort (List <T> list, Comparator <? Super T> c)

This method is also a generic method. Unlike the above method, there is no limit on the element types in the set. to sort the set, you must specify a Comparator, the generic type of this comparator must be the superclass (or itself) of the Set element ).

The above two methods can easily draw a simple conclusion that both Comparable and Comparator are used for comparison and sorting. If the element itself has implemented the Comparable interface, you can use it for comparative sorting. If the element itself does not implement the Comparable interface, you can use the external Comparator to compare and sort the elements. This is the sort that Comparable is used to sort the methods defined internally in the Set, and Comparator is used to sort the external implementations of the set. Next, let's look at the Comparable interface and Comparator interface.

 

  Next, let's look at the Comparable interface and Comparator interface.

Comparable

Public int compareTo (T o)

This interface only defines one compareTo method. In many "Value Type" data types, such as String, Integer, and Long, this interface has been implemented.

This method is similar to the equals method. equals emphasizes equal to no, while compareTo emphasizes more comparisons. If x <y,-1 is returned; if x = y, 0 is returned. If x> y, 1 is returned. When implementing this method, you also need to follow several rules:

If you are interested, you can view the compareTo method implementation in the String class. Its sorting rule is to convert the String into character arrays and sort them one by one in lexicographically.

 

Comparator

Int compare (T o1, T o2)

Boolean equals (Object obj)

This interface was greatly improved in JDK 8. Before JDK 8, only the above two methods were included, and JDK 8 reached 18 methods, all of which were the default methods of interfaces, and static methods.

In JDK8, this class is added@ FunctionalInterfaceFunction interfaces are annotated. Function interfaces indicate that only one method is included in the interface as the data type of Lambda expressions. This is mentioned in the new feature of JDK 8-Lambda expressions, java defines that if the method in the Object is overwritten, It is not counted, so there is only one compare method in the Comparator interface. The @ FunctionalInteface annotation can be added and cannot be added. To enable the compiler to perform better checks, only one method can be defined. This check will not be performed without the compiler.

The compare method is similar to the compareTo method. It also needs to meet the self-inverse, pass-through, and identity mentioned above. It also emphasizes that "(compare (x, y) = 0) = (x. equals (y)" is not strictly required.

For this class, we need to understand and learn the design patterns it uses --Rule Mode. The policy mode does not change the object itself, but uses another object to change its behavior. For example, in the supermarket price reduction operation, 10 items need to be reduced by one half. We can reduce the price of these 10 items to half, 10 items are easy to handle. If N items are needed, even we still need to reduce the price? In this case, we can use a "policy"-off for all products. This is to use another object to change the behavior of an object, rather than simply modifying the original object. Here, if the elements in the List do not implement the Comparable interface, but we need to sort it, we can modify the original object so that it can implement the Comparable interface, however, the modification code is not elegant. In this case, we can use the policy mode, that is, implement a Comparator interface to sort the elements in the set.

 

 

This is a public number that can add buff to programmers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.