How to convert Stream to an Array in Java

4 Answers

0 votes
import java.util.stream.Stream; 
import java.util.Arrays;

public class MyClass {
    public static void main(String args[]) {
        Stream<String> stream = Stream.of("java" , "c", "c++", "php", "c#"); 
   
        Object[] array = stream.toArray(); 
   
        System.out.println(Arrays.toString(array)); 
    }
}
   
   
   
   
/*
run:
   
[java, c, c++, php, c#]
   
*/

 



answered May 18, 2020 by avibootz
edited Mar 11, 2023 by avibootz
0 votes
import java.util.stream.Stream; 
import java.util.Arrays;

public class MyClass {
    public static void main(String args[]) {
        Stream<String> stream = Stream.of("java" , "c", "c++", "php", "c#"); 
 
        Object[] array = stream.toArray(Object[] ::new); 
 
        System.out.println(Arrays.toString(array)); 
    }
}
   

   
   
/*
run:
   
[java, c, c++, php, c#]
   
*/
   

 



answered May 18, 2020 by avibootz
edited Mar 11, 2023 by avibootz
0 votes
import java.util.stream.Stream; 
import java.util.Arrays;

public class MyClass {
    public static void main(String args[]) {
        Stream<String> stream = Stream.of("java" , "c", "c++", "php", "c#"); 
 
        String[] array = stream.toArray(size -> new String[size]);
 
        Arrays.stream(array).forEach(System.out::println);
    }
}
   
   
   
   
/*
run:
   
java
c
c++
php
c#
   
*/

 



answered May 18, 2020 by avibootz
edited Mar 11, 2023 by avibootz
0 votes
import java.util.stream.Stream; 
import java.util.Arrays;

public class MyClass {
    public static void main(String args[]) {
        Stream<String> stream = Stream.of("java" , "c", "c++", "php", "c#"); 
 
        String[] array = stream.toArray(String[]::new);
 
        Arrays.stream(array).forEach(System.out::println);
    }
}
   

   
   
/*
run:
   
java
c
c++
php
c#
   
*/
   

 



answered May 18, 2020 by avibootz
edited Mar 11, 2023 by avibootz

Related questions

1 answer 182 views
182 views asked Mar 23, 2023 by avibootz
1 answer 112 views
1 answer 216 views
1 answer 211 views
1 answer 261 views
2 answers 264 views
...