App下載
首頁(yè)javastream_convertJava Stream - 如何將Stream轉(zhuǎn)換為類型數(shù)組

Java Stream - 如何將Stream轉(zhuǎn)換為類型數(shù)組

我們想知道如何將Stream轉(zhuǎn)換為類型數(shù)組。
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));
    }    
}