Java Program to convert Stream to typed array

Java 8Object Oriented ProgrammingProgramming

Let us first create a Stream:

Stream<String> stream = Stream.of("Bing Bang Theory", "Vampire Diaries", "Game of Thrones", "Homecoming");

Now convert the above stream to typed array:

final String[] strArr = stream.toArray(String[]::new);

The following is an example to convert Stream to typed array in Java:

Example

import java.util.Arrays;
import java.util.stream.Stream;
public class Demo {
   public static void main(String[] args) {
      Stream<String> stream = Stream.of("Bing Bang Theory", "Vampire Diaries", "Game of Thrones", "Homecoming");
      final String[] strArr = stream.toArray(String[]::new);
      System.out.println("Array...");
      Arrays.asList(strArr).forEach(n-> System.out.println(n));
   }
}

Output

Array...
Bing Bang Theory
Vampire Diaries
Game of Thrones
Homecoming
raja
Published on 02-May-2019 11:48:37
Advertisements