Java Collections emptyListIterator() Method



Description

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.

Declaration

Following is the declaration for Java Collections emptyListIterator() method.

public static <T> ListIterator<T> emptyListIterator()

Parameters

NA

Return Value

NA

Exception

NA

Getting Empty ListIterator of Integers Example

The 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: false

Getting 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: false

Getting 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
Advertisements