How to Get Enumeration Over HashSet in Java


In this example, we will write an example to get Enumeration over HashSet with enumeration method of Collections class in Java.

Source Code

package com.beginner.examples;

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

public class GetEnumerationOverHashSet {

	public static void main(String[] args) {
		//create HashSet.
		HashSet hashSet = new HashSet();
		
		hashSet.add("A");
		hashSet.add("B");
		hashSet.add("C");
		
		/*
		 Get Enumeration over Java HashSet object using enumeration(Collection c) method of Collections.
		*/
		Enumeration enumeration = Collections.enumeration(hashSet);
		
		while(enumeration.hasMoreElements())
		      System.out.println(enumeration.nextElement());
		  }
		
}

Output:

A
B
C

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments