LongStream toArray()返回包含此流的元素的数组。这是终端操作,即可能会遍历流以产生结果或副作用。执行终端操作后,流管线被视为已消耗,无法再使用。
用法:
long[] toArray()
返回值:该函数返回一个包含此流元素的数组。
示例1:
// Java code for LongStream toArray()
import java.util.*;
import java.util.stream.LongStream;
class GFG {
// Driver code
public static void main(String[] args)
{
// Creating a LongStream
LongStream stream = LongStream.of(1L, 3L, 5L,
Long.MIN_VALUE);
// Using LongStream toArray()
long[] arr = stream.toArray();
// Displaying the elements in array arr
System.out.println(Arrays.toString(arr));
}
}
输出:
[1, 3, 5, -9223372036854775808]
示例2:
// Java code for LongStream toArray()
import java.util.*;
import java.util.stream.LongStream;
class GFG {
// Driver code
public static void main(String[] args)
{
// Creating a LongStream
LongStream stream = LongStream.range(5L, 12L);
// Using LongStream toArray()
long[] arr = stream.toArray();
// Displaying the elements in array arr
System.out.println(Arrays.toString(arr));
}
}
输出:
[5, 6, 7, 8, 9, 10, 11]
相关用法
- Java LongStream sum()用法及代码示例
- Java LongStream of()用法及代码示例
- Java LongStream iterator()用法及代码示例
- Java LongStream limit()用法及代码示例
- Java LongStream concat()用法及代码示例
- Java LongStream builder()用法及代码示例
- Java LongStream min()用法及代码示例
- Java LongStream findFirst()用法及代码示例
- Java LongStream max()用法及代码示例
- Java LongStream boxed()用法及代码示例
- Java LongStream parallel()用法及代码示例
- Java LongStream skip()用法及代码示例
- Java LongStream mapToDouble()用法及代码示例
- Java LongStream rangeClosed()用法及代码示例
- Java LongStream sequential()用法及代码示例
注:本文由纯净天空筛选整理自Sahil_Bansall大神的英文原创作品 LongStream toArray() in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。