How to Get Enumeration Over Vector in Java


In this example we will show how to get Enumeration over Vector with enumeration method of Collections class in Java. This tutorial would also help us learn how to enumerate through elements of Vector in Java.Collections class

Source Code

package com.beginner.examples;

import java.util.Collections;
import java.util.Enumeration;
import java.util.Vector;

public class GetEnumerationOverVector {

	public static void main(String[] args) {
		
		//create Vector 
		Vector vector = new Vector();
		
		vector.add("A");
		vector.add("B");
		vector.add("C");
		
		/*
		 Get Enumeration over Java Vector object using enumeration(Collection c) method of Collections.
		*/
		
		Enumeration enumeration = Collections.enumeration(vector);
		
		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