
在 Java 中,对包含 BigDecimal 类型的列表进行排序时,如果列表中存在 null 值,直接使用 Comparator.comparing 可能会导致 NullPointerException。 错误信息显示,即使使用了 Comparator.nullsFirst,仍然会出现空指针异常,这是因为 nullsFirst 作用于 ProductBenefitResponse 对象,而不是 BigDecimal 值本身。
为了解决这个问题,我们需要创建一个专门针对 BigDecimal 类型的 null 安全 Comparator,并将其应用于 thenComparing 方法中。
实现 Null 安全的 BigDecimal Comparator
首先,我们需要创建一个 Comparator<BigDecimal> 对象,该对象能够处理 null 值并进行自然排序。可以使用 Comparator.nullsFirst 和 Comparator.naturalOrder 组合来实现:
Comparator<BigDecimal> bigDecimalComparator = Comparator.nullsFirst(Comparator.naturalOrder());
这段代码创建了一个 bigDecimalComparator,它会将 null 值排在最前面,并使用 BigDecimal 的自然顺序进行排序。Comparator.naturalOrder() 实际上就是 BigDecimal::compareTo 的简写。
应用 Null 安全 Comparator 进行排序
接下来,将这个 bigDecimalComparator 应用到列表的排序操作中。假设我们有一个 ProductBenefitResponse 对象的列表 list,并且 ProductBenefitResponse 类有一个 getDescription() 方法返回 String 类型,以及一个 getBenefitLimit() 方法返回 BigDecimal 类型,我们可以这样排序:
List<ProductBenefitResponse> list = new ArrayList<>(benefitMap.values());
list.sort(
    Comparator
        .comparing(ProductBenefitResponse::getDescription)
        .thenComparing(ProductBenefitResponse::getBenefitLimit, bigDecimalComparator)
);在这里,thenComparing 方法接受一个额外的参数,即我们创建的 bigDecimalComparator。这样,在比较 benefitLimit 属性时,就会使用这个 null 安全的比较器,从而避免 NullPointerException。
完整示例代码
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
public class BigDecimalComparatorExample {
    public static void main(String[] args) {
        // 假设 benefitMap 是一个 Map<String, ProductBenefitResponse>
        Map<String, ProductBenefitResponse> benefitMap = Map.of(
            "key1", new ProductBenefitResponse("A", new BigDecimal("100.00")),
            "key2", new ProductBenefitResponse("B", null),
            "key3", new ProductBenefitResponse("A", new BigDecimal("50.00")),
            "key4", new ProductBenefitResponse("C", new BigDecimal("200.00"))
        );
        List<ProductBenefitResponse> list = new ArrayList<>(benefitMap.values());
        // 创建 BigDecimal 的 null 安全 Comparator
        Comparator<BigDecimal> bigDecimalComparator = Comparator.nullsFirst(Comparator.naturalOrder());
        // 使用 null 安全 Comparator 进行排序
        list.sort(
            Comparator
                .comparing(ProductBenefitResponse::getDescription)
                .thenComparing(ProductBenefitResponse::getBenefitLimit, bigDecimalComparator)
        );
        // 打印排序后的结果
        list.forEach(productBenefitResponse -> System.out.println(productBenefitResponse.getDescription() + ": " + productBenefitResponse.getBenefitLimit()));
    }
    static class ProductBenefitResponse {
        private String description;
        private BigDecimal benefitLimit;
        public ProductBenefitResponse(String description, BigDecimal benefitLimit) {
            this.description = description;
            this.benefitLimit = benefitLimit;
        }
        public String getDescription() {
            return description;
        }
        public BigDecimal getBenefitLimit() {
            return benefitLimit;
        }
        @Override
        public String toString() {
            return "ProductBenefitResponse{" +
                    "description='" + description + '\'' +
                    ", benefitLimit=" + benefitLimit +
                    '}';
        }
    }
}注意事项
总结
通过创建一个专门针对 BigDecimal 类型的 null 安全 Comparator,并将其应用于列表的排序操作中,可以有效地避免 NullPointerException,并确保排序结果的正确性。这种方法不仅适用于 BigDecimal,也可以推广到其他可能包含 null 值的类型的排序场景中。
以上就是BigDecimal 的 Null 安全 Comparator 实现的详细内容,更多请关注php中文网其它相关文章!
 
                        
                        每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
 
                Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号