Home
Map
Stream ExamplesExplore the Stream class. Call Arrays.stream and convert an ArrayList to a Stream.
Java
This page was last reviewed on Nov 7, 2021.
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.
IntStream.range
Arrays.stream. We begin with the Arrays.stream method. We must import the java.util.Arrays class. Stream() returns specially-typed streams.
Detail This is a special Stream of ints. It has powerful methods that act on ints in an optimized way.
Detail This is a Stream of doubles. The Arrays.stream method helpfully returns a DoubleStream.
Detail Some types do not have specialized Streams. We use a Stream of Strings but must specify the type in brackets.
Here We invoke the 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 stream. We can also get a stream from an ArrayList. The stream() method on an ArrayList instance returns a Stream object with a matching type.
ArrayList
However Stream() does not return an IntStream or DoubleStream, even when those types are present.
So We must use the 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
Sorted, 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.
Here We sort a 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 streams. A Random class returns one random number after another. With Random.ints() we get an IntStream of Random numbers. This is an infinite data source.
Random
A summary. With streams, we access powerful logic. We can transform, filter and reduce streams with method calls. Arrays and ArrayLists can be converted into streams.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Nov 7, 2021 (edit link).
Home
Changes
© 2007-2024 Sam Allen.