Java8 is pretty amazing. With lots of new features and Stream APIs Java8 is one of the best release we had last year.
In this tutorial we will go over how to convert Array to Stream
using Java8’s Arrays.stream
and Stream.of
operations.
Here are the steps:
- Create
Object Array
crunchifyCompany- Use Arrays.stream() and
- Stream.of() utility to covert Array to Stream
- Create
Primitive Array
doubleCrunchify- Use Arrays.stream() and
- Stream.of() ->
flatMapToDouble()
to covert Array to Stream
Java Code:
package crunchify.com.tutorial; import java.util.Arrays; import java.util.stream.DoubleStream; import java.util.stream.Stream; /** * @author Crunchify.com * */ public class CrunchifyArrayToStreamInJava8 { public static void main(String[] args) { // Try with Object Array String[] crunchifyCompany = { "Twitter", "Facebook", "Yahoo", "Google" }; // Arrays.stream - returns a sequential Stream with the specified array as its source Stream<String> crunchifyStream = Arrays.stream(crunchifyCompany); log("\n1. Arrays.stream output for Object Array:"); crunchifyStream.forEach(company -> log(company)); // Stream.of - returns a sequential ordered stream whose elements are the specified values Stream<String> crunchifyStream2 = Stream.of(crunchifyCompany); log("\n2. Stream.of output for Object Array:"); crunchifyStream2.forEach(company -> log(company)); // Now try with Primitive Arrays double[] doubleCrunchify = { 11.1, 21.2, 31.3, 41.4, 51.5 }; DoubleStream doubleStream = Arrays.stream(doubleCrunchify); log("\n1. Arrays.stream output for Primitive Arrays:"); doubleStream.forEach(company -> System.out.println(company)); Stream<double[]> crunchify = Stream.of(doubleCrunchify); // flatMapToDouble - returns an DoubleStream consisting of the results of replacing each element of this stream // with the contents of a mapped stream produced by applying the provided mapping function to each element. DoubleStream doubleStream2 = crunchify.flatMapToDouble(doubleArray -> Arrays.stream(doubleArray)); log("\n2. Stream.of output for Primitive Arrays:"); doubleStream2.forEach(company -> System.out.println(company)); } private static void log(String crunchifyString) { System.out.println(crunchifyString); } }
Result:
1. Arrays.stream output for Object Array: Twitter Facebook Yahoo Google 2. Stream.of output for Object Array: Twitter Facebook Yahoo Google 1. Arrays.stream output for Primitive Arrays: 11.1 21.2 31.3 41.4 51.5 2. Stream.of output for Primitive Arrays: 11.1 21.2 31.3 41.4 51.5