Tuesday, 9 May 2023

Get the enumeration from a Collection

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

Interview Questions

Collection programs in Java

Array programs in Java

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

Get the stream from Enumeration in Java

LinkedHashTable implementation in Java

No comments:

Post a Comment