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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。