Java 8中使用comparingInt处理空值问题

huangapple go评论99阅读模式
英文:

Java 8 comparingInt with null values

问题

我有一个功能,可以通过3个整数字段对POJO列表进行排序。

目前我正在使用comparingInt()thenComparingBy()

但是我可能会在中间得到一个空值。这会引发一个NullPointerException

为此,我希望将空值添加到末尾。Comparator.nullsLast()对我的情况不起作用,因为我是通过3个整数值进行比较的。

有没有办法实现这个...

英文:

I have a functionality to sort a list of pojo by 3 Integer fields.

Currently I am using comparingInt() and thenComparingBy().

But i may get a null value in between. This throws a NullPointerException.

For this i want to add null values at the end. Comparator.nullsLast() does not work for my case as I am comparing by 3 integer values.

Is there a way to achieve this....

答案1

得分: 5

因为您的组件 Integer 对象是 null,所以无法直接使用 comparingInt。您需要先创建一个支持处理 null 的整数比较器,然后在您的 Pojo 比较器中使用它。

Comparator<Integer> nullsLast = Comparator.nullsLast(Comparator.naturalOrder());
Comparator<Pojo> pojoComparator = Comparator.comparing(Pojo::getFirstInteger, nullsLast)
            .thenComparing(Pojo::getSecondInteger, nullsLast)
            .thenComparing(Pojo::getThirdInteger, nullsLast);
英文:

Since your component Integer objects are null you cannot use comparingInt with them. Instead of using comparingInt, first create a helper null-safe Integer comparator, and use that in your Pojo comparator.

Comparator&lt;Integer&gt; nullsLast = Comparator.nullsLast(Comparator.naturalOrder());
Comparator&lt;Pojo&gt; pojoComparator = Comparator.comparing(Pojo::getFirstInteger, nullsLast)
            .thenComparing(Pojo::getSecondInteger, nullsLast)
            .thenComparing(Pojo::getThirdInteger, nullsLast);

答案2

得分: 0

最简单的方法似乎是编写一个与排序特定 pojo 相关的比较器,针对适用于排序特定 pojo 的确切问题。

对我来说,这比将不符合您期望行为的部分(例如,comparingInt 不容忍空引用)粘合在一起要更明显。

英文:

The simplest way seems to be to write a comparator that's specific to the exact concerns that apply to sorting the particular pojo.

It seems to me more obvious than duct-taping parts together that don't behave how you want them (e.g., comparingInt not tolerating null references).

答案3

得分: 0

我知道这个问题已经有三年的历史了,但为什么你不直接为Comparing.comparingInt适配键提取器呢?

示例:

public static void main(String[] args) {
    Stream.of(4, 2, 7, 3, null, 1, 9)
        .sorted(Comparator.comparingInt(i -> Objects.nonNull(i) ? i : Integer.MAX_VALUE))
        .forEach(System.out::println);
}

在我的示例中,我有一个包含空值的 Int 流。要捕捉并为比较器进行适配,你只需在键提取器中进行检查。如果整数为null,你将其更改为另一个值。在这里,我选择使用 Integer.MAX_VALUE 将该元素放置在列表的末尾。

英文:

I know this question is already three years old, but why you don't just adapt the key extractor for Comparing.comparingInt?

Example:

public static void main(String[] args) {
    Stream.of(4, 2, 7, 3, null, 1, 9)
        .sorted(Comparator.comparingInt(i -&gt; Objects.nonNull(i) ? i : Integer.MAX_VALUE))
        .forEach(System.out::println);
}

In my example i have an Int-Stream with a null value. To catch and adapt it for the comparator you just have to check it in the key extractor. If the integer is null you change it to another value. Here i took Integer.MAX_VALUE to put the element to the end of the list.

huangapple
  • 本文由 发表于 2020年7月26日 21:30:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63100808.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定