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
使用用途仅限于测试、实验、研究为目的,禁止用于一切商业运营,本团队不承担使用者在使用过程中的任何违法行为负责所有源码请自测!不保证你源码完整性有效性所有源码都是全网搜集。客服QQ: 138338438
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
量推第二版房卡棋牌游戏 附带搭建架设视频教程(大联盟+俱乐部+比赛模式)
Volume promotion of the second edition of the room card chess game comes with a video tutorial on building and erecting (Major League + Club + Competition Mode)
Someone bought it 8 minutes ago 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