📜  Java中的 IntStream 迭代器()

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

Java中的 IntStream 迭代器()

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

句法 :

PrimitiveIterator.OfInt iterator()

Where, PrimitiveIterator.OfInt is an Iterator 
specialized for int values.

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

示例 1:

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

示例 2:

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