当前位置:   article > 正文

Java Comparator.comparing比较导致空指针异常_comparator.nullsfirst(comparator.naturalorder())

comparator.nullsfirst(comparator.naturalorder())

Comparator.comparing(Department::getOrder)

原因:

public static <T, U extends Comparable<? super U>> Comparator<T> comparing(
        Function<? super T, ? extends U> keyExtractor)
{
    Objects.requireNonNull(keyExtractor);
    return (Comparator<T> & Serializable)
        (c1, c2) -> keyExtractor.apply(c1).compareTo(keyExtractor.apply(c2));
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

如果keyExtractor.apply(c1),那么keyExtractor.apply(c1).compareTo(XX)将报空指针异常

替代方案
Comparator.comparing(Department::getOrder, Comparator.nullsFirst(Comparator.naturalOrder()))

替代方案好处:

public static <T, U> Comparator<T> comparing(
        Function<? super T, ? extends U> keyExtractor,
        Comparator<? super U> keyComparator)
{
    Objects.requireNonNull(keyExtractor);
    Objects.requireNonNull(keyComparator);
    return (Comparator<T> & Serializable)
        (c1, c2) -> keyComparator.compare(keyExtractor.apply(c1),
                                          keyExtractor.apply(c2));
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

会先取出keyExtractor.apply(c1)和keyExtractor.apply(c2),放入比较器进行比较
而Comparator.nullsFirst作为比较器,会创建一个Comparators.NullComparator比较器

public static <T> Comparator<T> nullsFirst(Comparator<? super T> comparator) {
    return new Comparators.NullComparator<>(true, comparator);
}
  • 1
  • 2
  • 3

Comparators.NullComparator比较器的compare接口实现中先进行空值判断处理,不为空的再进行代入比较器比较

/**
 * Null-friendly comparators
 */
final static class NullComparator<T> implements Comparator<T>, Serializable {
    private static final long serialVersionUID = -7569533591570686392L;
    private final boolean nullFirst;
    // if null, non-null Ts are considered equal
    private final Comparator<T> real;

    @SuppressWarnings("unchecked")
    NullComparator(boolean nullFirst, Comparator<? super T> real) {
        this.nullFirst = nullFirst;
        this.real = (Comparator<T>) real;
    }

    @Override
    public int compare(T a, T b) {
        if (a == null) {
            return (b == null) ? 0 : (nullFirst ? -1 : 1);
        } else if (b == null) {
            return nullFirst ? 1: -1;
        } else {
            return (real == null) ? 0 : real.compare(a, b);
        }
    }

    @Override
    public Comparator<T> thenComparing(Comparator<? super T> other) {
        Objects.requireNonNull(other);
        return new NullComparator<>(nullFirst, real == null ? other : real.thenComparing(other));
    }

    @Override
    public Comparator<T> reversed() {
        return new NullComparator<>(!nullFirst, real == null ? null : real.reversed());
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/141709
推荐阅读
相关标签
  

闽ICP备14008679号