连接两个流的 NullSafe 方法是什么?
What is NullSafe way to concatenate two Streams?
我有两个 整数流 guestsTravelWith 和 guests,我正在将它们连接起来,但是 当任何一个流为 Null 时抛出错误。是否有 nullsafe 方法来连接两个流?或者使用 if 条件是我唯一的希望?
Stream<Integer> guests = code+some_method();
Stream<Integer> guestsTravelWith = code+some_method();
Stream.concat(guestsTravelWith, guests)
如果您正在使用 Java 9 使用 Strem.ofNullable
Returns a sequential Stream containing a single element, if non-null,
otherwise returns an empty Stream.
一点都不好看,但是:
Stream.ofNullable(guestsTravelWith).orElse(Stream.empty()).flatMap(Function.identity())
或者你知道,“不太有趣”的方式:
guests == null ? Stream.empty() : guests;
您应该重新考虑 return null
Stream 开始的方法,这是一个糟糕的想法。
为什么不过滤掉 null
个流?
Stream<Integer> s1 = null;
Stream<Integer> s2 = Stream.of(1, 2, null, 4);
Stream.of(s1, s2)
.filter(Objects::nonNull) // Stream<Stream>
.flatMap(s -> s) // or Function.identity()
.forEach(System.out::println);
根据 Holger 的评论更新
Stream.concat
应替换为 Stream.of
和 null-safe Stream::flatMap
:
<R> Stream<R> flatMap(Function<? super T,? extends Stream<? extends R>> mapper)
Returns a stream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element. Each mapped stream is closed after its contents have been placed into this stream. (If a mapped stream is null an empty stream is used, instead.)
Stream<Integer> s1 = null;
Stream<Integer> s2 = Stream.of(1, 2, null, 4);
Stream.of(s1, s2)
.flatMap(s -> s) // or Function.identity()
.forEach(System.out::println);
输出:
1
2
null
4
我有两个 整数流 guestsTravelWith 和 guests,我正在将它们连接起来,但是 当任何一个流为 Null 时抛出错误。是否有 nullsafe 方法来连接两个流?或者使用 if 条件是我唯一的希望?
Stream<Integer> guests = code+some_method();
Stream<Integer> guestsTravelWith = code+some_method();
Stream.concat(guestsTravelWith, guests)
如果您正在使用 Java 9 使用 Strem.ofNullable
Returns a sequential Stream containing a single element, if non-null, otherwise returns an empty Stream.
一点都不好看,但是:
Stream.ofNullable(guestsTravelWith).orElse(Stream.empty()).flatMap(Function.identity())
或者你知道,“不太有趣”的方式:
guests == null ? Stream.empty() : guests;
您应该重新考虑 return null
Stream 开始的方法,这是一个糟糕的想法。
为什么不过滤掉 null
个流?
Stream<Integer> s1 = null;
Stream<Integer> s2 = Stream.of(1, 2, null, 4);
Stream.of(s1, s2)
.filter(Objects::nonNull) // Stream<Stream>
.flatMap(s -> s) // or Function.identity()
.forEach(System.out::println);
根据 Holger 的评论更新
Stream.concat
应替换为 Stream.of
和 null-safe Stream::flatMap
:
<R> Stream<R> flatMap(Function<? super T,? extends Stream<? extends R>> mapper)
Returns a stream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element. Each mapped stream is closed after its contents have been placed into this stream. (If a mapped stream is null an empty stream is used, instead.)
Stream<Integer> s1 = null;
Stream<Integer> s2 = Stream.of(1, 2, null, 4);
Stream.of(s1, s2)
.flatMap(s -> s) // or Function.identity()
.forEach(System.out::println);
输出:
1
2
null
4