Java Collections emptyListIterator() Method with Examples
Last Updated :
03 Jan, 2022
The emptyListIterator() method of Java Collections is a method that is used to iterate the List with no elements in Java.
Syntax:
public static <T> ListIterator<T> emptyListIterator()
Parameters: It has no parameters.
Return Type: It will return the list with empty elements.
Exceptions:
- NoSuchElementException – This will occur when the given element is not found in the given collection.
Example 1:
Java
import java.util.*;
public class GFG {
public static void main(String[] args)
{
ListIterator<String> iterator
= Collections.emptyListIterator();
System.out.println(iterator.hasNext());
}
}
|
Example 2:
Java
import java.util.*;
public class GFG {
public static void main(String[] args)
{
List<Integer> data = new ArrayList<Integer>();
data.add( 100 );
data.add( 200 );
ListIterator<String> iterator
= Collections.emptyListIterator();
System.out.println(iterator.next());
}
}
|
Output:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Collections$EmptyIterator.next(Collections.java:4191)
at GFG.main(GFG.java:12)
Example 3:
Java program to display next index and previous index element. We can do it by using the nextIndex() method to display the next index
Syntax:
iterator.nextIndex()
To display the previous index,
Syntax:
iterator.previousIndex()
Java
import java.util.*;
public class GFG {
public static void main(String[] args)
{
List<Integer> data = new ArrayList<Integer>();
data.add( 100 );
data.add( 200 );
ListIterator<String> iterator
= Collections.emptyListIterator();
System.out.println(iterator.nextIndex());
System.out.println(iterator.previousIndex());
}
}
|
Share your thoughts in the comments
Please Login to comment...