s-day07- 把Stream中的数据收集到数组中
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
package com.inmind.stream07;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/*
|
||||
把Stream中的数据收集到数组中
|
||||
|
||||
Object[] toArray() 返回一个包含此流的元素的数组。
|
||||
<A> A[] toArray(IntFunction<A[]> generator) 返回一个指定类型的数组
|
||||
*/
|
||||
public class Demo23 {
|
||||
public static void main(String[] args) {
|
||||
Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5);
|
||||
Object[] array = stream.toArray();
|
||||
System.out.println(Arrays.toString(array));
|
||||
//能不能在收集到数组中时,就确定类型呢???
|
||||
Stream<Integer> stream1 = Stream.of(1, 2, 3, 4, 5);
|
||||
Integer[] array1 = stream1.toArray(i -> new Integer[i]);
|
||||
System.out.println(Arrays.toString(array1));
|
||||
|
||||
Stream<String> stream2 = Stream.of("a", "b");
|
||||
String[] array2 = stream2.toArray(String[]::new);
|
||||
System.out.println(Arrays.toString(array2));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user