Java IntStream iterator()

Java IntStream iterator()

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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程