Streams
Streams implement the pipeline pattern, chaining operations into a single flow.
Some common uses are filtering and mapping:
final Collection<Wrapper> result;
result = strings.stream().filter(StringUtils::isNotBlank).map(Wrapper::new).collect(Collectors.toList());
Generation
Stream From Array
Stream<String> stream;
stream = Stream.of("abc", "def");
Stream From Iterable
Iterables don't give support to streams by default, but there is a utility class for this:
Iterable<String> strings;
Stream<String> stream;
stream = StreamSupport.stream(strings.spliterator(), false);
Empty Stream
Stream<String> empty;
empty = Stream.empty();
From Supplier
Stream<String> stream;
Supplier<String> supplier;
supplier = this::operation;
stream = Stream.generate(supplier);
Builder
Stream<String> stream;
Supplier<String> supplier;
supplier = this::operation;
stream = Stream.builder().add("text").build();
Iteration
Stream<Integer> stream;
// Numbers 1 to 10
stream = Stream.iterate(1, n -> n).limit(10);
From Other Streams
Stream<String> stream;
stream = Stream.concat(streamA, streamB);
Closing Streams
In some cases the streams can be working with an IO data source, or some other source which should be closed after being used. For that reason streams extend the AutoCloseable interface.
Last updated
Was this helpful?