Java Collections - emptyIterator() Method
The java.util.Collections.emptyIterator() method returns an iterator that has no elements. More precisely:
- hasNext always returns false.
- next always throws NoSuchElementException.
- remove always throws IllegalStateException.
Syntax
public static <T> Iterator<T> emptyIterator()
Here, T is the type of element, if there were any, in the iterator.
Parameters
No parameter is required.
Return Value
Returns an empty iterator.
Exception
NA.
Example:
In the example below, the java.util.Collections.emptyIterator() method is used to create an empty iterator.
import java.util.*; public class MyClass { public static void main(String[] args) { //creating an empty Iterator Iterator<Integer> Itr = Collections.emptyIterator(); //print the content of the Iterator while(Itr.hasNext()) { System.out.println(Itr.next()); } System.out.println("Iterator is empty."); } }
The output of the above code will be:
Iterator is empty.
❮ Java.util - Collections