Java 8 code to convert Stream to Array using Stream.toArray method
In this programming quick tip we will see the Java 8 code for converting from a
OUTPUT of the above code
Explanation of the code
In the above code we invoked the method
Where,
java.util.stream.Stream<T>
to an array of type T
using Stream.toArray()
method with explanation of the code.
Java 8 code to convert Stream<T>
to array of type T
Java 8 code to convert Stream<T> to array of type T
package com.javabrahman.java8.streams;
import static java.util.stream.Collectors.toList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
import com.javabrahman.java8.Employee;
public class StreamToArray {
public static void main(String args[]) {
List<String> stringList=Arrays.asList("Red", "Blue", "Green", "Orange", "Yellow");
Stream<String> stringStream= stringList.stream();
String[] stringArray=stringStream.toArray(size -> new String[size]);
System.out.println(Arrays.toString(stringArray));
}
}
[Red, Blue, Green, Orange, Yellow]
toArray()
on Stream<T>
interface to get an array of String
like this - String[] stringArray=stringStream.toArray(size -> new String[size])
- The overloaded
toArray()
method used above is passedsize -> new String[size]
as the parameter which is in fact a lambda expression(read tutorialLink to Lambda Expressions Tutorial.) - The
toArray()
method accepts a lambda expression as an input parameter;toArray()
’s method signature is -<A> A[] toArray(IntFunction<A[]> generator)
- I.e.
size -> new String[size]
is an instance of a functional interfaceIntFunction<R>
- The
IntFunction<A[]>
is a Java 8 Functional Interface(read tutorialClick to Read Detailed Article on Functional Interfaces) whose functional descriptor is defined asint -> R
for functional method with signatureR apply(int value)
- I.e. the
IntFunction<A[]>
takes anint
input and returns a result of typeR
. - Putting it all together, in our case, the
IntFunction<A[]>
has been passed the lambda expressionsize -> new String[size]
. This lambda returns aString[]
array of lengthsize
based on inputsize
passed to it. And thisString[]
array is what we get back from theStream.toArray()
method as the desired result.
Java 8 Streams API tutorials on JavaBrahman
Streams API - Introduction & BasicsClick to Read tutorial on Streams API Basics Understanding Stream Operations | Intermediate and Terminal OperationsClick to Read Tutorial on Stream Operations Overview Mapping with Streams using map and flatMap methodsClick to Read how Mapping with Java8 Streams works Filtering and Slicing with Streams using filter,distinct,limit,skip methodsClick to Read how Filtering and Slicing with Java8 Streams works Matching with Streams using allMatch,anyMatch,noneMatch methodsClick to Read tutorial on matching with Streams API Stream API's findFirst,findAny methods' tutorialClick to Read tutorial on findFirst() and findAny() methods of Streams API 'Peeking' into a running Stream with Stream.peek() methodClick to Read tutorial on Stream.peek() method Creating Infinite Streams with iterate\generate methodsClick to Read tutorial on Creating Infinite Streams Reducing Streams using Streams.reduce methodClick to Read tutorial on Reducing Streams
Streams API - Introduction & BasicsClick to Read tutorial on Streams API Basics Understanding Stream Operations | Intermediate and Terminal OperationsClick to Read Tutorial on Stream Operations Overview Mapping with Streams using map and flatMap methodsClick to Read how Mapping with Java8 Streams works Filtering and Slicing with Streams using filter,distinct,limit,skip methodsClick to Read how Filtering and Slicing with Java8 Streams works Matching with Streams using allMatch,anyMatch,noneMatch methodsClick to Read tutorial on matching with Streams API Stream API's findFirst,findAny methods' tutorialClick to Read tutorial on findFirst() and findAny() methods of Streams API 'Peeking' into a running Stream with Stream.peek() methodClick to Read tutorial on Stream.peek() method Creating Infinite Streams with iterate\generate methodsClick to Read tutorial on Creating Infinite Streams Reducing Streams using Streams.reduce methodClick to Read tutorial on Reducing Streams