The source code is for testing only and should not be used commercially. The source code comes from the Internet. If there is any infringement, please contact me to remove it.
Java 中的Comparator和Comparable

1. preface

previous article easy, right java.util.Comparator After explaining it, today let's look at another interface that is very similar to it java.lang.Comparable

2. Comparable

Comparable An interface also has only one abstract method int compareTo(T o), its rules andComparator theint compare(T o1, T o2)similar. Although it can also be viewed as a functional interface, Java 8 It is not marked as a functional interface in. Explain that the designer does not want developers to use it as a functional interface. Otherwise, you deviate from the design intent, like typing a string below and returning the length of the string.

//Operations that conform to the syntax but do not conform to the design intent
Comparable<String> comparable = String::length;

under normal circumstancesComparable Instances of an object that you want to express as a characteristic of the object are compared with each other. For example, movies have the feature of comparing by year.

class Movie implements Comparable<Movie> { 
    private double rating; 
    private String name; 
    private int year; 

    // Used to sort movies by year 
    public int compareTo(Movie m){ 
        return this.year - m.year; 
    } 
}

Comparable is often used for natural sorting, that is, the elements themselves are comparable.

3.Comparator vs Comparable

Comparator and Comparable They are very similar, but they also have some differences, mainly in the following:

  • Different perspectives,Comparable It is usually a comparison attribute that comes with the object Comparator It is usually compared as a "third party".
  • usually ComparableNeeds to be implemented by objects to be used as features, andComparator More like strategy.
  • one in java.lang Under the package, one is in java.util Next, this also proves the first rule from the side.

4. summary

In summary, if the ordering of objects needs to be based on natural order (which is itself comparable), use the Comparable, and if you need to sort different attributes based on your business, use the Comparator

read more
Resource download
PriceFree
Customer Service QQ: 138338438
Original link:https://bcbccb.cn/en/4618.html, please indicate the source for reprinting.
1

Comments0

趣味麻将棋牌游戏平台(量推定制版)
Fun Mahjong chess and card game platform (customized version)
Someone just purchased Go and have a look

Site Announcements

The source code (theme/plug-in/application source code) and other resources provided by this site are only for learning and exchange

Commercial use is prohibited, otherwise all consequences will be borne by the downloading user!

Some resources are collected or copied online. If they infringe on your legitimate rights and interests, please write to us.

Currently, members have a big reward, and the current price for a lifetime member is 299 gold coins.Recent price adjustments

Join quickly, opportunities wait for no one! immediately participated in

Captcha

Fast login to social accounts

en_USEnglish