HomeSearch

Java Stream: Arrays.stream and ArrayList stream

Explore the Stream class. Call Arrays.stream and convert an ArrayList to a Stream.

Stream.

The water is still. Not a wave appears. The myths of a sea monster living beneath are false. As you turn to leave a tentacle grabs (with great force) your foot.

As a Java developer,

you must think fast. With the stream() method we can create a Stream from an array or ArrayList. With lambdas we can manipulate the stream.

Arrays.stream.

Every second matters now. We begin with the Arrays.stream method. We must import the java.util.Arrays class. Stream() returns specially-typed streams.

IntStream: This is a special Stream of ints. It has powerful methods that act on ints in an optimized way.

DoubleStream: This is a Stream of doubles. The Arrays.stream method helpfully returns a DoubleStream.

Stream: 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.

Lambdas
Java program that uses Arrays.stream, IntStream 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); } } Output 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."

Java program that uses ArrayList, Stream 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); } } Output 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.

Java program that uses sorted, forEach 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)); } } Output 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.Range

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: ints

With streams,

we access powerful logic. We can transform, filter and reduce streams with method calls. Often we must first get the stream from a source.

Arrays and ArrayLists

can be converted into streams. This increases our ability to mutate and test these data sources. A stream() call is required.
Home
Dot Net Perls
© 2007-2019 Sam Allen. All rights reserved. Written by Sam Allen, info@dotnetperls.com.