In this post, we will talk about the first two methods – creating Stream
object from a collection and from an array.
In Java streams basics, we used the List<String>
object as the source. A Stream<String>
object was created by calling Collection
method stream()
. Here is a variation of the same example:
List.of("one","two","three")
.stream()
.map(s -> s.length())
.filter(i -> i == 3)
.forEach(System.out::print); //prints: 33
Since the method stream()
belongs to the Collection
interface, all Java interfaces that extend Collection
and all the classes that implement it, have this method.
To ensure backward compatibility, the Java 8 authors took advantage of a new feature introduced with Java 8 – the ability of an interface to contain an implementation as default
method. Such an implementation can be overridden by the class that implements the interface. Otherwise, it will behave as if the class did implement it. The default implementation looks as follows:
default Stream<E> stream() {
return StreamSupport.stream(spliterator(), false);
}
The second boolean
parameter of the StreamSupport.stream()
method indicates whether the stream is going to be parallel or not. Correspondingly, there is another default
method in the Collection
interface:
default Stream<E> parallelStream() {
return StreamSupport.stream(spliterator(), true);
}
This time, as you can see, the second parameter of the StreamSupport.stream()
method is true
. We will talk about parallel streams in one of the installments later.
Using the stream()
method of the Collection
interface is one of the most popular ways to create a Stream
object. There is a lot of legacy code that creates Java collections and passes them as parameters.
Creating a stream from an array is easy too. The java.util.Array
has the following eight overloaded versions that convert an array or a part of it into a stream of the corresponding data types:
static DoubleStream stream(double[] array);
static DoubleStream stream(double[] array,
int startInclusive, int endExclusive);
static IntStream stream(int[] array);
static IntStream stream(int[] array,
int startInclusive, int endExclusive);
static LongStream stream(long[] array);
static LongStream stream(long[] array,
int startInclusive, int endExclusive);
static Stream stream(T[] array);
static Stream stream(T[] array,
int startInclusive, int endExclusive);
The following are examples of the usage:
String[] arr = {"one","two","three","four","five"};
Arrays.stream(arr)
.map(s -> s.length())
.filter(i -> i == 3)
.forEach(System.out::print); //prints: 33
Arrays.stream(arr, 1, 4)
.map(s -> s.length())
.filter(i -> i == 3)
.forEach(System.out::print); //prints: 3
We will continue talking about stream creation in the following installment of the series. In the next post, we discuss creating a stream using method Stream.of().
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.