死磕Lambda表达式(五):Comparator复合

2022-10-09,,,,

给岁月以文明,而不是给文明以岁月。——《三体》

在上一篇文章(传送门)中介绍了jdk为我们提供的常用函数式接口,jdk不仅提供的这些函数式接口,其中一些接口还为我们提供了实用的默认方法,这次我们来介绍一下comparator复合

欢迎关注微信公众号:万猫学社,每周一分享java技术干货。

comparator的使用

在之前文章的例子中,我们使用comparator.comparing静态方法构建了一个comparator接口的实例,我们再来简单介绍一下。先来看一下mask类是怎么写的:

package one.more.study;

/**
 * 口罩
 * @author 万猫学社
 */
public class mask {
    public mask() {
    }
    
    public mask(string brand, string type, double price) {
        this.brand = brand;
        this.type = type;
        this.price = price;
    }

    /**
     * 品牌
     */
    private string brand;
    /**
     * 类型
     */
    private string type;
    /**
     * 价格
     */
    private double price;

    public string getbrand() {
        return brand;
    }

    public void setbrand(string brand) {
        this.brand = brand;
    }

    public string gettype() {
        return type;
    }

    public void settype(string type) {
        this.type = type;
    }

    public double getprice() {
        return price;
    }

    public void setprice(double price) {
        this.price = price;
    }

    @override
    public string tostring() {
        return "mask{" +
                "brand='" + brand + '\'' +
                ", type='" + type + '\'' +
                ", price=" + price +
                '}';
    }
}

然后,根据口罩品牌对口罩列表进行正序排序:

list<mask> masklist = new arraylist<>();
masklist.add(new mask("3m", "kn95",17.8));
masklist.add(new mask("honeywell", "kn95",18.8));
masklist.add(new mask("3m", "ffp2",19.8));
masklist.add(new mask("honeywell", "n95",19.5));
masklist.sort(comparator.comparing(mask::getbrand));
for (mask mask : masklist) {
    system.out.println(mask);
}

运行结果如下:

mask{brand='3m', type='kn95', price=17.8}
mask{brand='3m', type='ffp2', price=19.8}
mask{brand='honeywell', type='kn95', price=18.8}
mask{brand='honeywell', type='n95', price=19.5}

欢迎关注微信公众号:万猫学社,每周一分享java技术干货。

逆序

需求改了,要求按照口罩品牌进行逆序排列,这是还需不需要再构建一个comparator接口的实例呢?答案是不需要,comparator接口有一个默认方法reversed可以使其逆序,把上面的例子稍微修改一下:

masklist.sort(comparator.comparing(mask::getbrand).reversed());

运行结果如下:

mask{brand='honeywell', type='kn95', price=18.8}
mask{brand='honeywell', type='n95', price=19.5}
mask{brand='3m', type='kn95', price=17.8}
mask{brand='3m', type='ffp2', price=19.8}

欢迎关注微信公众号:万猫学社,每周一分享java技术干货。

比较器链

需求又改了,先按照口罩品牌逆序排序,如果口罩品牌一样,再按照口罩类型正序排序。comparator接口还有一个默认方法thencomparing就是做这个的,它的入参也是一个function接口的实例,如果前一个比较器的比较结果相同,就当前的比较器再进行比较,我们再来修改一下上面的例子:

masklist.sort(comparator.comparing(mask::getbrand)
        .reversed()
        .thencomparing(mask::gettype));

运行结果如下:

mask{brand='honeywell', type='kn95', price=18.8}
mask{brand='honeywell', type='n95', price=19.5}
mask{brand='3m', type='ffp2', price=19.8}
mask{brand='3m', type='kn95', price=17.8}

需求又又改了,先按照口罩品牌逆序排序,如果口罩品牌一样,再按照口罩价格正序排序。口罩价格是double类型,如果使用thencomparing会导致自动装箱,造成资源的白白浪费。所以,推荐使用thencomparingdouble方法,它的入参是todoublefunction,代码修改如下:

masklist.sort(comparator.comparing(mask::getbrand)
        .reversed()
        .thencomparingdouble(mask::getprice));

运行结果如下:

mask{brand='honeywell', type='kn95', price=18.8}
mask{brand='honeywell', type='n95', price=19.5}
mask{brand='3m', type='kn95', price=17.8}
mask{brand='3m', type='ffp2', price=19.8}

类似这样支持基础数据类型的方法还有两个:thencomparingint方法,它的入参是tointfunctionthencomparinglong方法,它的入参是tolongfunction

欢迎关注微信公众号:万猫学社,每周一分享java技术干货。

总结

默认方法名称 作用 入参 入参签名
reversed 逆序
thencomparing 比较器链 function (t) -> r
thencomparingint 比较器链 tointfunction (t) -> int
thencomparinglong 比较器链 tolongfunction (t) -> long
thencomparingdouble 比较器链 todoublefunction (t) -> double

死磕lambda表达式》系列

  • 死磕lambda表达式(一):初识lambda
  • 死磕lambda表达式(二):lambda的使用
  • 死磕lambda表达式(三):更简洁的lambda
  • 死磕lambda表达式(四):常用的函数式接口
  • 死磕lambda表达式(五):comparator复合
  • 死磕lambda表达式(六):consumer、predicate、function复合

微信公众号:万猫学社

微信扫描二维码

获得更多java技术干货

《死磕Lambda表达式(五):Comparator复合.doc》

下载本文的Word格式文档,以方便收藏与打印。