The Java Collections enumeration(Collection<T>) method is used to get an enumeration over the specified collection.
DeclarationFollowing is the declaration for java.util.Collections.enumeration() method.
public static <T> Enumeration<T> enumeration(Collection<T> c)Parameters
c − This is the collection for which an enumeration is to be returned.
Return ValueThe method call returns an enumeration over the specified collection.
ExceptionNA
Getting Enumeration to Iterate Collection of Integers ExampleThe following example shows the usage of Java Collection enumeration() method to get an enumeration on list of Integers. We've created a list of integers and navigated to each element using the enumeration object created using enumeration() method.
package com.tutorialspoint; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Enumeration; import java.util.List; public class CollectionsDemo { public static void main(String args[]) { // create array list object List<Integer> arrlist = new ArrayList<>(Arrays.asList(1,2,3)); // create Enumeration Enumeration<Integer> e = Collections.enumeration(arrlist); System.out.println("Print the enumeration"); while(e.hasMoreElements()) { System.out.println("Value is: "+e.nextElement()); } } }Output
Let us compile and run the above program, this will produce the following result.
Print the enumeration Value is: 1 Value is: 2 Value is: 3Getting Enumeration to Iterate Collection of Strings Example
The following example shows the usage of Java Collection enumeration() method to get an enumeration on list of strings. We've created a list of strings and navigated to each element using the enumeration object created using enumeration() method.
package com.tutorialspoint; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Enumeration; import java.util.List; public class CollectionsDemo { public static void main(String args[]) { // create array list object List<String> arrlist = new ArrayList<>(Arrays.asList("A","B","C")); // create Enumeration Enumeration<String> e = Collections.enumeration(arrlist); System.out.println("Print the enumeration"); while(e.hasMoreElements()) { System.out.println("Value is: "+e.nextElement()); } } }Output
Let us compile and run the above program, this will produce the following result.
Print the enumeration Value is: A Value is: B Value is: CGetting Enumeration to Iterate Collection of Objects Example
The following example shows the usage of Java Collection enumeration() method to get an enumeration on list of Student objects. We've created a list of student objects and navigated to each element using the enumeration object created using enumeration() method.
package com.tutorialspoint; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Enumeration; import java.util.List; public class CollectionsDemo { public static void main(String args[]) { // create array list object List<Student> arrlist = new ArrayList<>(Arrays.asList(new Student(1, "Julie"), new Student(2, "Robert"),new Student(3, "Adam"))); // create Enumeration Enumeration<Student> e = Collections.enumeration(arrlist); System.out.println("Print the enumeration"); while(e.hasMoreElements()) { System.out.println("Value is: "+e.nextElement()); } } } 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.
Print the enumeration Value is: [ 1, Julie ] Value is: [ 2, Robert ] Value is: [ 3, Adam ]
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