Java Stream to Array Example
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...!!! Click To Tweet
Example 1. Convert Stream to String Array using toArray()
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | /** * 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
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | /** * 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
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | /** * 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...!!! Click To Tweet
Do you like this Post? – then check my other helpful posts:
- Convert a Stream to a List in Java 8
- Stream maptoint in Java 8 with examples
- Double the numbers of specified ArrayList using Streams
- Double the even / odd numbers of a specified ArrayList using Streams
- How to check if Number is Prime or not using Streams
- Retrieve Even/Odd Numbers within the Range using Java 8 Streams
- How to add/sum up the ArrayList integers using Java8 Streams
- Generate Prime Numbers in Java 8 using Streams
- Comparator example – Collections sort with/without Lambda in Java 8
- How to pass function as a parameter in a method in Java 8?
- Remove duplicates from ArrayList in Java 8
- ForEach examples for Map/List in Java 8
Other Useful References:
- java.util.stream by Oracle
Source: https://techndeck.com/how-to-convert-a-stream-to-array-java-8/
0 Response to "Java Stream to Array Example"
Post a Comment