两者均为null时工作Comparator.nullsFirst [英] Working of Comparator.nullsFirst when both are null

查看:277
本文介绍了两者均为null时工作Comparator.nullsFirst的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有几个可选"(不是java.util.Optional)字段的类.我正在编写一个Lambda比较器,通过比较它们的属性子集来测试是否相等

I have a class with several "optional" (not java.util.Optional) fields. I was writing a Lambda comparator to test for equality by comparing a subset of their attributes

我写了

private final static Comparator<MyEntity> COMPARATOR_491 =  
            comparing(MyEntity::getIsin)
             .thenComparing(MyEntity::getTMarketType)
             .thenComparing(nullsFirst(comparing(MyEntity::getIsoCode)))
             .thenComparing(MyEntity::getTaxRate)
             .thenComparing(nullsFirst(comparing(MyEntity::getEndDate)));

  • ISIN不为空
  • 市场类型不为空
  • 代码可以为空
  • 税率不为空
  • 结束日期可以为空
    • ISIN is not null
    • Market type is not null
    • Is code can be null
    • Tax rate is not null
    • End date can be null
    • 问题是我经常得到一个NullPointerException.这是(几乎不可读)堆栈跟踪

      The problem is that often I get a NullPointerException. This is the (barely readable) stack trace

      java.lang.NullPointerException: null
          at java.util.Comparator.lambda$comparing$77a9974f$1(Comparator.java:469) ~[?:1.8.0_51]
          at java.util.Comparator$$Lambda$40/221702765.compare(Unknown Source) ~[?:?]
          at java.util.Comparators$NullComparator.compare(Comparators.java:83) ~[?:1.8.0_51]
          at java.util.Comparator.lambda$thenComparing$36697e65$1(Comparator.java:217) ~[?:1.8.0_51]
          at java.util.Comparator$$Lambda$42/770739971.compare(Unknown Source) ~[?:?]
          at java.util.Comparator.lambda$thenComparing$36697e65$1(Comparator.java:216) ~[?:1.8.0_51]
          at java.util.Comparator$$Lambda$42/770739971.compare(Unknown Source) ~[?:?]
          at java.util.Comparator.lambda$thenComparing$36697e65$1(Comparator.java:216) ~[?:1.8.0_51]
          at java.util.Comparator$$Lambda$42/770739971.compare(Unknown Source) ~[?:?]
      

      我发现两个示例实体的结束日期有所不同.第一个实体具有非null属性,第二个实体具有null

      I found that two sample entities differ by the End date. The first entity has non-null property, the second has null

      我认为当一个或两个参数为null时,nullsFirst比较器可能是null安全的.

      I thought that nullsFirst comparer could be null safe when one or both arguments are null.

      我在做什么错了?

      推荐答案

      nullsFirst(…)返回的比较器返回一个比较器,该比较器处理要比较的一个或两个对象为null的情况.

      The comparator returned by nullsFirst(…) returns a comparator which handles the case that one or both of the objects to compare are null.

      因此,当您说nullsFirst(comparing(MyEntity::getIsoCode))时,您将得到一个比较器来处理一个或两个MyEntity实例均为null的情况,并根据其自然顺序比较getIsoCode属性(不处理null值) ,如果两个MyEntity实例都不是null.

      So when you say nullsFirst(comparing(MyEntity::getIsoCode)), you get a comparator handling the case that either or both MyEntity instances are null and comparing the getIsoCode property according to their natural order (not handling null values), if neither MyEntity instance is null.

      要实现的是comparing(MyEntity::getIsoCode, nullsFirst(naturalOrder())),请指定用于比较属性值的null安全比较器. getEndDate属性也是如此.

      What you want achieve, is comparing(MyEntity::getIsoCode, nullsFirst(naturalOrder())), specify the null-safe comparator to be used to compare the property values. The same applies to the getEndDate property.

      您可以将其与thenComparing融合到previousComparator.thenComparing(MyEntity::getIsoCode, nullsFirst(naturalOrder()))

      这篇关于两者均为null时工作Comparator.nullsFirst的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆