Get Enumeration Over Java ArrayList
Last Updated :
15 Nov, 2022
ArrayList class is a re-sizeable array that is present in java.util package. Unlike the built-in arrays, ArrayList can change their size dynamically where elements can be added and removed from an ArrayList.
In java version 1, enum was not present. With advancement to version, 1.5 enum was introduced in java which is not as same as seen in C/C++ so do the functionality differs. Enum is basically used to constrict the range for example months, age, or be it a string (considering name of a company) which can not be changed as final values initially itself. In java version 1, enumeration was carried out by interfaces. After version 1.5 it was introduced in java as discussed so simply importing enum class makes working with enum easy simply by importing classes.
ArrayList can be of any type as shown below. The enumeration method of java.util.Collections class is used to return an enumeration over the specified collection.
- An ArrayList of integer, string, double, etc.
- ArrayList of ArrayLists, or simply an ArrayList of HashMaps.
- An ArrayList of any user-defined objects.
Syntax: For declaring an enumeration
public static Enumeration enumeration(Collection c) ;
Method Used: hasMoreElements() Method
An object that implements the Enumeration interface generates a series of elements, one at a time. hasMoreElements() method of enumeration used to tests if this enumeration contains more elements. If enumeration contains more elements than it will return true else false.
Syntax:
boolean hasMoreElements() ;
Parameters: This method accepts nothing.
Return value: This method returns true if and only if this enumeration object contains at least one more element to provide; false otherwise.
Implementation:
Example 1
Java
// Getting Enumeration over Java ArrayList
// Importing ArrayList,Collection and Enumeration class
// from java.util package
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
// Class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating object of ArrayList
// String type here- shoppingList
ArrayList<String> shoppingList = new ArrayList<>();
// Adding element to ArrayList
// Custom inputs
shoppingList.add("Perfume");
shoppingList.add("Clothes");
shoppingList.add("Sandal");
shoppingList.add("Jewellery");
shoppingList.add("Watch");
// Creating object of type Enumeration<String>
Enumeration e
= Collections.enumeration(shoppingList);
// Condition check using hasMoreElements() method
while (e.hasMoreElements())
// print the enumeration
System.out.println(e.nextElement());
}
}
OutputPerfume
Clothes
Sandal
Jewellery
Watch
Example 2
Java
// Getting Enumeration over Java ArrayList
// Importing ArrayList,Collection and Enumeration class
// from java.util package
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
// Class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating object of ArrayList<String
ArrayList<String> LanguageList = new ArrayList<>();
// Adding element to LanguageList
// Custom inputs
LanguageList.add("Java");
LanguageList.add("C++");
LanguageList.add("Kotlin");
LanguageList.add("Scala");
LanguageList.add("Ruby");
LanguageList.add("Python");
LanguageList.add("C#");
// Creating object of type Enumeration<String>
Enumeration e
= Collections.enumeration(LanguageList);
// Condition check using hasMoreElements()
while (e.hasMoreElements())
// Print the enumeration
System.out.println(e.nextElement());
}
}
OutputJava
C++
Kotlin
Scala
Ruby
Python
C#
Similar Reads
Get Enumeration over Java Vector In java, the vector is a typical dynamic array whose size can increase or decrease. While in array the size cannot be changed after declaration. We have to include file import java.util.Vector to use Vector and store values in it. Also, import java.util.Enumeration to use enumeration. Approach 1: Co
3 min read
Get Enumeration over Java HashSet The HashSet class implements the Set interface, backed by a hash table which is a HashMap instance. There is no assurance as to the iteration order of the set, which implies that over time, the class does not guarantee the constant order of elements. The null element is allowed by this class. The ja
2 min read
Convert ArrayList to Vector in Java There are several ways to convert ArrayList to Vector. We can use a vector constructor for converting ArrayList to vector. We can read ArrayList elements one by one and add them in vector. Approach 1: (Using Vector Constructor) Create an ArrayList.Add elements in ArrayList.Create a vector and pass t
3 min read
Java Program to Create ArrayList From Enumeration Enumerations serve the purpose of representing a group of named constants in a programming language. Enums are used when we know all possible values at compile-time, such as choices on a menu, rounding modes, command-line flags, etc. It is not necessary that the set of constants in an enum type stay
2 min read
How to Add Element in Java ArrayList? Java ArrayList class uses a dynamic array for storing the elements. It is like an array, but there is no size limit. We can add or remove elements anytime. So, it is much more flexible than the traditional array. Element can be added in Java ArrayList using add() method of java.util.ArrayList class.
2 min read
Traverse Through ArrayList in Forward Direction in Java ArrayList is a part of the collection framework and is present in java.util package. It provides us with dynamic arrays in Java. The listIterator() method of java.util.ArrayList class is used to return a list iterator over the elements in this list (in a proper organized sequence). ArrayList can be
3 min read