Other Operations
There are a few operations that do not classify as either an intermediate operation or a terminal operation. These are shown in the table below:
Operation | Action |
---|---|
|
Returns |
|
Closes the |
isParallel
Stream.of("B", "A", "C", "B")
.parallel()
.isParallel()
returns true
because the Stream
is parallel.
Stream.of("B", "A", "C", "B")
.sequential()
.isParallel()
returns false
because the Stream
is not parallel.
close
Stream<String> stream = Stream.of("B", "A", "C", "B");
stream.forEachOrdered(System.out::println);
stream.close();
prints all elements in the Stream
and then closes the Stream
. Some streams (e.g. streams from files) need to be closed to release their resources. Use the try-with-resource patterns if the Stream
must be closed:
try (Stream<String> s = Stream.of("B", "A", "C", "B")) {
s.forEachOrdered(System.out::println);
}