The Java Collections emptyListIterator() method is used to get the empty list iterator. ListIterator is empty and its hasNext method always returns false. next() method call throws NoSuchElementException and remove() throws IllegalStateException.
DeclarationFollowing is the declaration for Java Collections emptyListIterator() method.
public static <T> ListIterator<T> emptyListIterator()Parameters
NA
Return ValueNA
ExceptionNA
Getting Empty ListIterator of Integers ExampleThe following example shows the usage of Java Collection emptyListIterator() method to get an empty list iterator of Integers. We've created an empty list iterator using emptyListIterator() method and then checked if list iterator has elements or not.
package com.tutorialspoint; import java.util.Collections; import java.util.ListIterator; public class CollectionsDemo { public static void main(String args[]) { // create an empty list ListIterator<Integer> emptyListIterator = Collections.emptyListIterator(); System.out.println("Created empty list iterator, it has elements: "+emptyListIterator.hasNext()); } }Output
Let us compile and run the above program, this will produce the following result.
Created empty list iterator, it has elements: falseGetting Empty ListIterator of Strings Example
The following example shows the usage of Java Collection emptyListIterator() method to get an empty list iterator of Strings. We've created an empty list iterator using emptyListIterator() method and then checked if list iterator has elements or not.
package com.tutorialspoint; import java.util.Collections; import java.util.ListIterator; public class CollectionsDemo { public static void main(String args[]) { // create an empty list ListIterator<String> emptyListIterator = Collections.emptyListIterator(); System.out.println("Created empty list iterator, it has elements: "+emptyListIterator.hasNext()); } }Output
Let us compile and run the above program, this will produce the following result.Exception will be thrown as the list is immutable.
Created empty list iterator, it has elements: falseGetting Empty ListIterator of Objects Example
The following example shows the usage of Java Collection emptyListIterator() method to get an empty list iterator of Student objects. We've created an empty list iterator using emptyListIterator() method and then checked if list iterator has elements or not.
package com.tutorialspoint; import java.util.Collections; import java.util.ListIterator; public class CollectionsDemo { public static void main(String args[]) { // create an empty list ListIterator<Student> emptyListIterator = Collections.emptyListIterator(); System.out.println("Created empty list iterator, it has elements: "+emptyListIterator.hasNext()); } } class Student { int rollNo; String name; Student(int rollNo, String name){ this.rollNo = rollNo; this.name = name; } @Override public String toString() { return "[ " + this.rollNo + ", " + this.name + " ]"; } }Output
Let us compile and run the above program, this will produce the following result.Exception will be thrown as the list is immutable.
Created empty list iterator, it has elements: false
java_util_collections.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4