Stream
With the Java stream()
method we can create a Stream
from an array or ArrayList
. With lambdas we can manipulate the stream.
When using streams, we must change our approach to processing data. We call methods, one after another, to change the output. Many transformations are possible.
Arrays.stream
We begin with the Arrays.stream
method. We must import the java.util.Arrays
class
. Stream()
returns specially-typed streams.
IntStream
is a special Stream
of ints. It has powerful methods that act on ints in an optimized way.DoubleStream
is a Stream
of doubles. The Arrays.stream
method helpfully returns a DoubleStream
.Stream
of Strings but must specify the type in brackets.anyMatch
method on the three streams. We test for a condition in each case. All calls here return true.import java.util.Arrays; import java.util.stream.DoubleStream; import java.util.stream.IntStream; import java.util.stream.Stream; public class Program { public static void main(String[] args) { // Use Arrays.stream to create an IntStream. int[] array = { 10, 20, 30 }; IntStream stream = Arrays.stream(array); // ... Call anyMatch on the IntStream. boolean result = stream.anyMatch(number -> number >= 25); System.out.println(result); // Create a DoubleStream. double[] array2 = { 1.2, 1.3, 1.4 }; DoubleStream stream2 = Arrays.stream(array2); // ... Test the DoubleStream. boolean result2 = stream2.anyMatch(number -> number >= 1.35); System.out.println(result2); // Create a Stream of Strings. String[] array3 = { "cat", "dog", "bird" }; Stream<String> stream3 = Arrays.stream(array3); // ... Test the strings. boolean result3 = stream3.anyMatch(value -> value.length() >= 4); System.out.println(result3); } }true true true
ArrayList
streamWe can also get a stream from an ArrayList
. The stream()
method on an ArrayList
instance returns a Stream
object with a matching type.
Stream()
does not return an IntStream
or DoubleStream
, even when those types are present.stream()
directly when we get it from an ArrayList
. We "cannot cast from a Stream
(int
) to an IntStream."import java.util.ArrayList; import java.util.stream.Stream; public class Program { public static void main(String[] args) { // Create an Integer ArrayList and add three numbers to it. ArrayList<Integer> list = new ArrayList<Integer>(); list.add(10); list.add(200); list.add(3000); // Get a Stream from the ArrayList. Stream<Integer> stream = list.stream(); // Test the Stream. boolean result = stream.allMatch(value -> value >= 5); System.out.println(result); } }true
forEach
A Stream
can be sorted with the sorted()
method. With strings, this sorts from first to last (from "A" to "Z"). With integers it goes also from low to high.
String
Stream
(of bird names) and then with forEach
display each element.import java.util.ArrayList; import java.util.stream.Stream; public class Program { static void display(String value) { // Called on each string. System.out.println("Value: " + value); } public static void main(String[] args) { // Create list of birds. ArrayList<String> list = new ArrayList<>(); list.add("parrot"); list.add("sparrow"); list.add("finch"); list.add("canary"); // Get the stream and call sorted on it. Stream<String> sorted = list.stream().sorted(); // ... Call display method on each element in sorted order. sorted.forEach(x -> display(x)); } }Value: canary Value: finch Value: parrot Value: sparrow
IntStream.Range
Suppose you want a range of numbers like 1, 2, 3. With the static
method IntStream.range
(and rangeClosed
) we can easily generate this.
Random
streamsA Random
class
returns one random number after another. With Random.ints()
we get an IntStream
of Random
numbers. This is an infinite data source.
With streams, we access powerful logic. We can transform, filter and reduce streams with method calls. Arrays and ArrayLists
can be converted into streams.