( empty() IntStream, LongStream DoubleStream, Stream .)
: empty() API, - , - . null, - . , , Stream.empty() .
, :
List<Integer> list =
IntStream.range(0, 10)
.flatMap(i -> (i & 1) == 0 ? IntStream.of(i, i) : IntStream.empty())
.boxed()
.collect(Collectors.toList());
[0, 0, 2, 2, 4, 4, 6, 6, 8, 8]
as expected. The main thing is that flatMap()it goes into one value and expects to get an arbitrary number of values, including zero values. The way this is done is for the flat matching operation to return a stream of values. So that it returns null values, it returns an empty stream.
source
share