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
The use is limited to testing, experiments, and research purposes. It is prohibited for all commercial operations. This team is not responsible for any illegal behavior of users during use. Please self-test all source codes! There is no guarantee of the integrity and validity of your source code. All source code is collected from the entire network
Original link:https://bcbccb.cn/en/4618.html, please indicate the source for reprinting. Disclaimer: This resource has not been authorized by the original rights holder and is not commercially available. It can only be used to learn and analyze the underlying code, CSS, etc., and is prohibited for commercial purposes. Any relevant disputes and legal liabilities arising from unauthorized commercial use shall be fully borne by the user. Everyone is responsible to support genuine copies. Please delete them within 24 hours after downloading. Thank you for your support!
1

Comments0

【最新更新】ZN7号_微盘wp二开版带余额宝功能时间盘黄金期货数字火币交易+完整数据+k线正常
[Latest update] ZN No. 7_microdisk wp second edition with balance treasure function Time disk gold futures digital hot coin trading + complete data +k line normal
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