Create an ArrayList using an Enumeration from a vector

Problem Statement:

How to Create an ArrayList using an Enumeration from a vector?

 Create an ArrayList using an Enumeration from a vector

Description:

Step1: Constructs an empty vector studentVector  so that its internal data array has size

Step2:  Appends the specified element- Student Names to the end of studentVector Vector.

Step3:  create an Enumeration using Vector elements

Step4: Create ArrayList from Enumeration of Vector

  • Returns an array list studentList containing the elements returned by the specifie enumeration studentEnumeration in the order they are returned by the enumeration studentEnumeration.
  • Collections.list method provides interoperability between legacy APIs that return enumerations and new APIs that require collections.
  • Create ArrayList from Enumeration of Vector

Create an ArralyList using an Enumeration from a vector

Sample Code to Create an ArrayList using an Enumeration from a vector

package com.stixs.java.collections;

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

/**
 * The Class STXCreateArrayListFromEnumeration - example is to create an
 * ArralyList using an Enumeration from a vector. This is done using list method
 * of Collections class.
 */
public class STXCreateArrayListFromEnumeration {

 // Main Classs
 public static void main(String[] args) {

 // Constructs an empty vector so that its internal data array has size
 // 10 and its standard capacity increment is zero.
 Vector<String> studentVector = new Vector<String>();

 //Appends the specified element- Student Names to the end of studentVector Vector.
 studentVector.add("Allan"); 
 studentVector.add("Black");
 studentVector.add("Den");
 studentVector.add("Porta");
 studentVector.add("Anuva");

 System.out.println("STXCreateArrayListFromEnumeration:: Student names in the "
 + "studentVector in the Vector: " + studentVector);

 /*
 * studentVector.elements() - Returns an enumeration of the components
 * of studentVector vector. 
 * 
 * The returned studentEnumeration object will generate all items in studentVector vector. 
 * The first item generated is the item at index 0, then the item at index 1, and so on.
 * 
 * create an Enumeration using Vector elements
 */

 Enumeration<String> studentEnumeration = studentVector.elements();

 /*
 * Returns an array list studentList containing the elements returned by
 * the specified enumeration studentEnumeration in the order they are
 * returned by the enumeration studentEnumeration.
 * 
 * Collections.list method provides interoperability between legacy APIs
 * that return enumerations and new APIs that require collections.
 * Create ArrayList from Enumeration of Vector
 */
 ArrayList<String> studentList = Collections.list(studentEnumeration);

 System.out.println("STXCreateArrayListFromEnumeration:: Student names in the "
 + "studentList after creating ArrayList : " + studentList);

 }
}

seperator

Output:

STXCreateArrayListFromEnumeration:: Student names in the 
studentVector: [Allan, Black, Den, Porta, Anuva]
STXCreateArrayListFromEnumeration:: Student names in the 
studentList after creating ArrayList : [Allan, Black, Den, Porta, Anuva]

seperator

Java API Classes and Methods used to Create an ArrayList using an Enumeration from a vector

Vector.add()

Vector.add

Vector.elements()

Vector.elements

Collections Class

Collections Class

Collections.list()

Collections.list

IMG SRC: From Oracle Java Docs

seperator

Reference

Refer below path to get the complete details about Java Collections class.

https://docs.oracle.com/javase/8/docs/api/java/util/Collections.html

seperator

You may also like this

seperator

About TechnoStixs.com

Create an ArrayList using an Enumeration from a vector

TechnoStixs.com is a dedicated weblog for Java/J2EE and web developers. We take pride in our work. Every publication is carefully analyzed, written and tested to ease the understanding of the topic or subject.

We cover Java Core Technologies, eCommerce, J2EE Frameworks, Web Service, Build Tools, Unit Test Frameworks and Others.

1,449 total views, 1 views today

Add comment