Java NavigableSet iterator() Method Tutorial

In this section, we will learn what the NavigableSet iterator() method is and how to use it in Java.

What is Java NavigableSet iterator() Method?

The Java NavigableSet iterator() method is used to return an Iterator object by which we can iterate through the elements of a NavigableSet object.

Note: please check the Iterator interface section if you’re not familiar with this interface.

Java NavigableSet iterator() Method Syntax:

Iterator<E> iterator()

iterator() Method Parameters:

The method does not take an argument.

iterator() Method Return Value:

The return value of this method is an object of type Iterator interface.

iterator() Method Exceptions:

The method does not throw an exception.

Example: using NavigableSet iterator() method

import java.util.NavigableSet;
import java.util.TreeSet;
import java.util.Iterator;
class Main{
    public static void main(String[] args) {
        NavigableSet<Integer> navigate = new TreeSet<>();
        navigate.add(1);
        navigate.add(2);
        navigate.add(3);
        navigate.add(0);
        navigate.add(10);

        Iterator <Integer> iterate = navigate.iterator();
        while (iterate.hasNext()){
            System.out.println(iterate.next());
        }
    }
        
}

Output:

0

1

2

3

10
Facebook
Twitter
Pinterest
LinkedIn

Top Technologies