📜  Java中的 LongStream 迭代器()

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

Java中的 LongStream 迭代器()

LongStream iterator()返回此流元素的迭代器。它是一个终端操作,即它可以遍历流以产生结果或副作用。

句法 :

PrimitiveIterator.OfLong iterator()

Where, PrimitiveIterator.OfLong is an Iterator 
specialized for long values.

返回值: LongStream iterator() 返回此流的元素迭代器。

示例 1:

// Java code for LongStream iterator()
import java.util.*;
import java.util.stream.LongStream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // Creating an LongStream
        LongStream stream = LongStream.of(2L, 4L, 6L, 8L);
  
        // Using LongStream iterator() to return
        // an iterator for elements of the stream
        PrimitiveIterator.OfLong answer = stream.iterator();
  
        // Displaying the stream elements
        while (answer.hasNext()) {
            System.out.println(answer.nextLong());
        }
    }
}
输出:
2
4
6
8

示例 2:

// Java code for LongStream iterator()
import java.util.*;
import java.util.stream.LongStream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // Creating an LongStream which includes
        // lower bound element but excludes
        // upper bound element
        LongStream stream = LongStream.range(2L, 8L);
  
        // Using LongStream iterator() to return
        // an iterator for elements of the stream
        PrimitiveIterator.OfLong answer = stream.iterator();
  
        // Displaying the stream elements
        while (answer.hasNext()) {
            System.out.println(answer.nextLong());
        }
    }
}
输出:
2
3
4
5
6
7