Open In App

IntStream iterator() Method in Java

Last Updated : 07 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Java, streams are used to process sequences of data efficiently. IntStream is a special type of stream which is designed to handle primitive int values. This avoids the need for converting the primitive types into objects.

The iterator() method in IntStream is a terminal operation that gives us an iterator, and with the help of this iterator, we can iterate over the elements of the stream.

Declaration of IntStream iterator()

The declaration of the IntStream iterator() is:

PrimitiveIterator.OfInt iterator()

  • Parameter: This method does not take any parameters.
  • Return type: This method returns an iterator of type PrimitiveIterator.OfInt. It is a special iterator which is used for iterating over primitive int values in the stream.

Note: The iterator() method is a terminal operation.

What is Terminal Operation?

A terminal operation is one that consumes the stream. Once a terminal operation is invoked, the stream is considered consumed, and it can no longer be used. The iterator() method is a terminal operation because it consumes the stream and returns an iterator, which allows us to iterate over the elements.

Now, we are going to discuss some examples for better understanding.

Example 1: Using iterator() with IntStream.of()

Java
// Java program to demonstrate the working of 
// iterator() with IntStream.of()
import java.util.*;
import java.util.stream.IntStream;

class Geeks {
    
    public static void main(String[] args) {
        
        // Creating an IntStream
        IntStream s = IntStream.of(2, 4, 6, 8);

        // Using IntStream iterator() to return an 
        // iterator for elements of the stream
        PrimitiveIterator.OfInt i = s.iterator();

        // Displaying the stream elements
        while (i.hasNext()) {
            System.out.println(i.nextInt());
        }
    }
}

Output
2
4
6
8

Explanation: Here, we are creating an IntStream and use the iterator() method to get an iterator.

Note: The hasNext() method is used to check if there are more elements and nextInt() method is used to get the next element in the stream.

Example 2: Using iterator() with IntStream.range()

Java
// Java program to demonstrate the working of 
// iterator() with IntStream.range()
import java.util.*;
import java.util.stream.IntStream;

class Geeks {
    
    public static void main(String[] args) {
        
        // Creating an IntStream using range()
        IntStream s = IntStream.range(2, 8);

        // Using IntStream iterator() to return an 
        // iterator for elements of the stream
        PrimitiveIterator.OfInt i = s.iterator();

        // Displaying the stream elements
        while (i.hasNext()) {
            System.out.println(i.nextInt());
        }
    }
}

Output
2
3
4
5
6
7

Explanation: Here, we are using IntStream.range() method for creating a stream of integers from a start point to an end point and then we are using an iterator to iterate over the stream.


Why use IntStream iterator() ?

The iterator() method in IntStream is useful when we need more control over how we iterate through the stream. The reasons for using IntStream iterator is listed below:

  • For large streams, using an iterator can be more efficient because it lets us handle one element at a time.
  • Sometimes we may need to pass an iterator to code that requires it instead of a stream.

Important Points:

The iterator() method is a terminal operator which means that once the iterator has been created and consumed, the original stream cannot be used again for further operations. If we need to perform additional operations on the stream after using iterator(), we need to recreate the stream.



Practice Tags :

Similar Reads