Java Program to convert Stream to typed array


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

Updated on: 30-Jul-2019

195 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements