Java Collections emptyEnumeration() Method



Description

The Java Collections emptyEnumeration() method is used to get the empty enumeration. Enumeration is empty and its hasMoreElements method always returns false.

Declaration

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

public static final <T> Enumeration<T> emptyEnumeration()

Parameters

NA

Return Value

NA

Exception

NA

Getting Empty Enumeration of Integers Example

The following example shows the usage of Java Collection emptyEnumeration() method to get an empty enumeration of Integers. We've created an empty enumeration using emptyEnumeration() method and then checked if enumeration has elements or not.

 
package com.tutorialspoint;

import java.util.Collections;
import java.util.Enumeration;

public class CollectionsDemo {
   public static void main(String args[]) {
      
      // create an empty list    
      Enumeration<Integer> emptyEnumeration = Collections.emptyEnumeration();

      System.out.println("Created empty enumeration, it has elements: "+emptyEnumeration.hasMoreElements());
   }    
}

Output

Let us compile and run the above program, this will produce the following result.

Created empty enumeration, it has elements: false

Getting Empty Enumeration of Strings Example

The following example shows the usage of Java Collection emptyEnumeration() method to get an empty enumeration of Strings. We've created an empty enumeration using emptyEnumeration() method and then checked if enumeration has elements or not.

 
package com.tutorialspoint;

import java.util.Collections;
import java.util.Enumeration;

public class CollectionsDemo {
   public static void main(String args[]) {
      
      // create an empty list    
      Enumeration<String> emptyEnumeration = Collections.emptyEnumeration();

      System.out.println("Created empty enumeration, it has elements: "+emptyEnumeration.hasMoreElements());
   }    
}

Output

Let us compile and run the above program, this will produce the following result.

Created empty enumeration, it has elements: false

Getting Empty Enumeration of Objects Example

The following example shows the usage of Java Collection emptyEnumeration() method to get an empty enumeration of Student objects. We've created an empty enumeration using emptyEnumeration() method and then checked if enumeration has elements or not.

 
package com.tutorialspoint;

import java.util.Collections;
import java.util.Enumeration;

public class CollectionsDemo {
   public static void main(String args[]) {
      
      // create an empty list    
      Enumeration<Student> emptyEnumeration = Collections.emptyEnumeration();

      System.out.println("Created empty enumeration, it has elements: "+emptyEnumeration.hasMoreElements());
   }    
}
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.

Created empty enumeration, it has elements: false
java_util_collections.htm
Advertisements