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

本文详细介绍Java中Comparator接口的使用,包括如何构建Comparator实例,以及如何利用其默认方法reversed和thenComparing实现复杂排序需求,如逆序排序和比较器链。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

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}

逆序

需求改了,要求按照口罩品牌进行逆序排列,这是还需不需要再构建一个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}

比较器链

需求又改了,先按照口罩品牌逆序排序,如果口罩品牌一样,再按照口罩类型正序排序。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

总结

默认方法名称作用入参入参签名
reversed逆序
thenComparing比较器链Function(T) -> R
thenComparingInt比较器链ToIntFunction(T) -> int
thenComparingLong比较器链ToLongFunction(T) -> long
thenComparingDouble比较器链ToDoubleFunction(T) -> double

《死磕Lambda表达式》目录

文章持续更新,微信搜索「 万猫学社 」第一时间阅读。

评论 50
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

万猫学社

您的鼓励将是我创作的最大动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值