A Stream
object can be created in several ways, such as, for example:
— from a Collection using the stream() and parallelStream() methods;
— from an array using Arrays.stream(Object[]);
— from an array or varargs using Stream.of(Object[]);
— using Stream.builder();
— using Stream.generate(Supplier);
— using Stream.iterate(Object, UnaryOperator) or Stream.iterate(T seed, Predicate hasNext, UnaryOperator f);
— using IntStream.range(int, int), IntStream.rangeClosed(int, int) or LongStream.range(long, long), LongStream.rangeClosed(long, long);
— using Files.lines(Path) that creates Stream<String> of lines from the file;
— using Files.list(Path) or Files.walk(Path) that create Stream<Path> of entries in the directory;
— using String class methods chars(), codePoints(), and lines();
— using IntStream Random.ints() or any of its other overloaded versions that create a stream of random ints; also, LongStream Random.longs(), DoubleStream Random.doubles() and their overloads;
— using various other classes of JDK, including IntStream BitSet.stream(), JarFile.stream() (opens in a new tab)”>Stream<JarEntry> JarFile.stream(), and Pattern.splitAsStream(java.lang.CharSequence) (opens in a new tab)”>Stream<String> Pattern.splitAsStream(java.lang.CharSequence), to name a few.
We will discuss each of these methods in the subsequent posts.
See other posts on Java 8 streams and posts on other topics.
You can also use navigation pages for Java stream related blogs:
— Java 8 streams blog titles
— Create stream
— Stream operations
— Stream operation collect()
The source code of all the code examples is here in GitHub.
Send your comments using the link Contact or in response to my newsletter.
If you do not receive the newsletter, subscribe via link Subscribe under Contact.