首页javastream_convertJava Stream - 如何将Stream转换为类型数组

Java Stream - 如何将Stream转换为类型数组

我们想知道如何将Stream转换为类型数组。
import java.util.Arrays;
import java.util.stream.Stream;

public class Main {
    public static void main(String[] args) {
      // collect as typed array
      Stream<String> words = Stream.of("All", "men", "are", "created", "equal");
      final String[] wordsArray = words.toArray(String[]::new);
      Arrays.asList(wordsArray).forEach(n-> System.out.println(n));
    }    
}