Java stream轉陣列

  • stream
  • Array

stream轉陣列

public static void main(String[] args) {
	Stream<String> stream1 = Stream.of("Hello","Duke","Java");
	Stream<Integer> stream2 =Stream.of(1,2,3,4);
	String[] array1 = stream1.toArray(String[]::new);
	Integer[] array2 =  stream2.toArray(Integer[]::new);
	
}

IntStream轉陣列

public static void main(String[] args) {
	int [] array1 = IntStream.range(0, 5).toArray();
	Integer [] array2 = IntStream.range(0, 5).boxed().toArray(Integer[] ::new);
}

結論

留意當前stream,若為IntStream則可直接使用toArray方法

若為Stream<T>,則需進行陣列的生成

 

Referance