In this tutorial, we will see “How to convert a Stream to an Array in Java 8?” stream to array java

Convert a Stream to an Array in Java with example...!!! Share on X

Example 1. Convert Stream to String Array using toArray()

/**
 * Convert Stream to String Array using Object Array with toArray() method.
 * @author Deepak Verma
 */
import java.util.Arrays;
import java.util.stream.Stream;

public class Convert_Stream_to_String_Array {

    public static void main(String[] args) {

    	//Stream of type String
    	Stream<String> students = Stream.of("Tom", "Adam", "Chris", "Henry", "Jason"); 
    	
    	//Convert Stream to Array Object
    	Object[] objectArray = students.toArray(); 
    	
    	//Returns the string representation of the contents of the 'objectArray'
    	System.out.println("Stream to Array using toArray():"); 
    	System.out.println(Arrays.toString(objectArray));

    }

}

 

Output:

Stream to Array using toArray():
[Tom, Adam, Chris, Henry, Jason]

 

Example 2. Convert Stream to Integer Array using Lambda

/**
 * Convert Stream to Integer Array using Lambda
 * @author Deepak Verma
 */
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

public class Convert_Stream_to_Integer_Array_Using_Lambda {

    public static void main(String[] args) {

    	// via - Stream.toArray() and lambda expression 
    	Integer[] intNumbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 
    	List<Integer> listOfIntegers = Arrays.asList(intNumbers); 

    	//Convert List of Integers to Stream and then Stream to Array using Lambda
    	Integer[] array1 = (Integer[]) listOfIntegers.stream()
    			                                    .toArray(x -> new Integer[x]); 
    	
    	
    	System.out.println("List of Integer to Stream and then to Integer array using Lambda:"); 
    	System.out.println(Arrays.asList(array1));
    	
    	//OR
    	
    	//Stream of Integers
    	Stream<Integer> streamOfIntegers = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
    	
    	//Convert Stream to Array using Lambda
    	Integer[] array2 = streamOfIntegers.toArray(x -> new Integer[x]); 

    	System.out.println("\nStream to Integer array using Lambda:"); 
        //Either use this
    	System.out.println(Arrays.asList(array2));
    	//Or this
    	System.out.println(Arrays.toString(array2));

    }

}

 

Output:

List of Integer to Stream and then to Integer array using Lambda:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Stream to Integer array using Lambda:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

 

Example 3. Convert Stream to Integer Array using Method Reference

/**
 * Convert Stream to Integer Array using Method Reference
 * @author Deepak Verma
 */
import java.util.Arrays;
import java.util.stream.Stream;

public class Convert_Stream_to_Integer_Array_using_Method_Reference {

    public static void main(String[] args) {

    	//Stream of Integers
    	Stream<Integer> streamOfIntegers = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
    	
    	//Convert Stream to Array using Method Reference
    	Integer[] array = streamOfIntegers.toArray(Integer[]::new); 

    	System.out.println("Stream to Integer array using Method Reference:"); 
    	System.out.println(Arrays.toString(array));
    	
    }

}

 

Output:

Stream to Integer array using Method Reference:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

stream to array java

stream to array java stream to array java stream to array java stream to array java stream to array java

Convert a Stream to an Array in Java with example...!!! Share on X

Do you like this Post? – then check my other helpful posts:

Other Useful References:

Author

  • Deepak Verma

    Deepak Verma is a Test Automation Consultant and Software development Engineer for more than 10 years. His mission is to help you become an In-demand full stack automation tester.

    He is also the founder of Techndeck, a blog and online coaching platform dedicated to helping you succeed with all the automation basics to advanced testing automation tricks.

    View all posts