Using Collections.enumeration method, we can get an enumeration over the specified collection.
Signature
public static <T> Enumeration<T> enumeration(final Collection<T> c)
Example
Enumeration<Integer> enumeration = Collections.enumeration(primesCollection);
Find the below working application.
EnumerationFromCollectionDemo.java
package com.sample.app.collections;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
public class EnumerationFromCollectionDemo {
public static void main(String[] args) {
Collection<Integer> primesCollection = Arrays.asList(2, 3, 5, 7, 11);
Enumeration<Integer> enumeration = Collections.enumeration(primesCollection);
while (enumeration.hasMoreElements()) {
System.out.println(enumeration.nextElement());
}
}
}
Output
2 3 5 7 11
You may like
Get a composite unmodifiable array list from two arrays
Join the collection elements by a separator using streams
Get the stream from an iterator in Java
No comments:
Post a Comment