You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ArrayList对象按属性排序遇空指针异常,求Fleet类排序实现方案

兄弟,我太懂这种按对象属性排序触发NPE的痛苦了!之前踩过好多次坑,大概率是没处理空集合、空元素这些边界情况。下面给你完整的解决方案,直接拿去用,还帮你避开所有可能的NPE坑:

完整解决方案

1. Fleet类实现

首先是要求的Fleet公共类,包含私有int变量,以及必须的getter方法(排序时需要访问这些私有属性):

public class Fleet {
    // 私有实例变量
    private int considerable;
    private int complicated;

    // 构造器,方便快速创建实例
    public Fleet(int considerable, int complicated) {
        this.considerable = considerable;
        this.complicated = complicated;
    }

    // 必须的getter方法,排序逻辑需要获取属性值
    public int getConsiderable() {
        return considerable;
    }

    public int getComplicated() {
        return complicated;
    }

    // 可选:toString方法,方便调试查看排序结果
    @Override
    public String toString() {
        return "Fleet{" +
                "considerable=" + considerable +
                ", complicated=" + complicated +
                '}';
    }
}

2. ProblemSet类中的sortFleet方法(带空指针防护)

这个方法专门处理了几种会触发NPE的场景:传入的集合为null、集合中混有null的Fleet对象,同时实现了按属性排序的逻辑(这里默认按considerable升序,再按complicated降序,你可以根据需求修改):

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class ProblemSet {

    // 公共静态方法sortFleet,接收ArrayList<Fleet>,返回排序后的List<Fleet>
    public static List<Fleet> sortFleet(ArrayList<Fleet> fleetList) {
        // 先处理传入的集合为null的情况,直接返回空列表避免NPE
        if (fleetList == null) {
            return new ArrayList<>();
        }

        // 1. 过滤掉集合中的null元素,避免调用getter时触发NPE
        // 2. 按指定属性排序:先按considerable升序,再按complicated降序
        return fleetList.stream()
                .filter(fleet -> fleet != null)
                .sorted(Comparator.comparingInt(Fleet::getConsiderable)
                        .thenComparing(Comparator.comparingInt(Fleet::getComplicated).reversed()))
                .collect(Collectors.toList());
    }

    // 测试用例,验证功能和NPE防护
    public static void main(String[] args) {
        ArrayList<Fleet> fleets = new ArrayList<>();
        fleets.add(new Fleet(3, 5));
        fleets.add(null); // 故意加入null元素测试过滤逻辑
        fleets.add(new Fleet(1, 10));
        fleets.add(new Fleet(3, 2));

        List<Fleet> sortedFleets = sortFleet(fleets);
        sortedFleets.forEach(System.out::println);
        // 输出结果:
        // Fleet{considerable=1, complicated=10}
        // Fleet{considerable=3, complicated=5}
        // Fleet{considerable=3, complicated=2}
    }
}

3. 为什么之前会触发NPE?

给你梳理几个常见的踩坑点:

  • 传入的ArrayList为null:如果直接调用fleetList.stream()或者Collections.sort(fleetList),立刻会触发NPE,所以我们先判断集合是否为null。
  • 集合中存在null的Fleet对象:如果直接用Comparator.comparingInt(Fleet::getConsiderable),碰到null对象时调用getter方法会抛出NPE,所以必须先过滤掉null元素。
  • 传统排序写法未做空校验:如果不用Stream API,用Collections.sort的话,也要在Comparator里手动处理null对象,比如:
// 传统写法的安全Comparator示例
Collections.sort(fleetList, (f1, f2) -> {
    if (f1 == null && f2 == null) return 0;
    if (f1 == null) return 1; // 把null元素放到排序结果的最后
    if (f2 == null) return -1;
    // 先比较considerable属性
    int compareResult = Integer.compare(f1.getConsiderable(), f2.getConsiderable());
    if (compareResult != 0) {
        return compareResult;
    }
    // 再按complicated降序比较
    return Integer.compare(f2.getComplicated(), f1.getComplicated());
});

这样处理后,就能彻底避免排序时的空指针异常了。

内容的提问来源于stack exchange,提问作者wallischpls

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:11:45