Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Get the enumeration from a Collection

Using Collections.Enumeration method, we can get an enumeration over the specified collection.

 

Signature

public static Enumerationenumeration(final Collection c)

Example

Enumeration 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 primesCollection = Arrays.asList(2, 3, 5, 7, 11);

		Enumeration 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



This post first appeared on Java Tutorial : Blog To Learn Java Programming, please read the originial post: here

Share the post

Get the enumeration from a Collection

×

Subscribe to Java Tutorial : Blog To Learn Java Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×