📜  Java中的 LongStream toArray()

📅  最后修改于: 2022-05-13 01:55:38.616000             🧑  作者: Mango

Java中的 LongStream toArray()

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]