Enumeration Interface In Java
Last Updated :
15 Dec, 2021
java.util.Enumeration interface is one of the predefined interfaces, whose object is used for retrieving the data from collections framework variable( like Stack, Vector, HashTable etc.) in a forward direction only and not in the backward direction. This interface has been superceded by an iterator.
The Enumeration Interface defines the functions by which we can enumerate the elements in a collection of elements. For new code, Enumeration is considered obsolete. However, several methods of the legacy classes such as vectors and properties, several API classes, application codes use this Enumeration interface.
Important Features
- Enumeration is Synchronized.
- It does not support adding, removing, or replacing elements.
- Elements of legacy Collections can be accessed in a forward direction using Enumeration.
- Legacy classes have methods to work with enumeration and returns Enumeration objects.
Declaration
public interface Enumeration<E>
Where E is the type of elements stored in a Collection.
The sub-interfaces of Enumeration interface is NamingEnumeration and implementing class is StringTokenizer.
Creating Enumeration Object
Vector ve = new Vector();
Enumeration e = v.elements();
Example:
Java
// Java program to test Enumeration
import java.util.Vector;
import java.util.Enumeration;
public class EnumerationClass {
public static void main(String args[])
{
Enumeration months;
Vector<String> monthNames = new Vector<>();
monthNames.add("January");
monthNames.add("February");
monthNames.add("March");
monthNames.add("April");
monthNames.add("May");
monthNames.add("June");
monthNames.add("July");
monthNames.add("August");
monthNames.add("September");
monthNames.add("October");
monthNames.add("November");
monthNames.add("December");
months = monthNames.elements();
while (months.hasMoreElements()) {
System.out.println(months.nextElement());
}
}
}
OutputJanuary
February
March
April
May
June
July
August
September
October
November
December
Java Enumeration Interface With SequenceInputStream
Java
import java.io.*;
import java.util.*;
class GFG {
public static void main(String args[])
throws IOException
{
// creating the FileInputStream objects for all the
// files
FileInputStream fin
= new FileInputStream("file1.txt");
FileInputStream fin2
= new FileInputStream("file2.txt");
FileInputStream fin3
= new FileInputStream("file3.txt");
FileInputStream fin4
= new FileInputStream("file4.txt");
// creating Vector object to all the stream
Vector v = new Vector();
v.add(fin);
v.add(fin2);
v.add(fin3);
v.add(fin4);
// creating enumeration object by calling the
// elements method
Enumeration e = v.elements();
// passing the enumeration object in the constructor
SequenceInputStream bin
= new SequenceInputStream(e);
int i = 0;
while ((i = bin.read()) != -1) {
System.out.print((char)i);
}
bin.close();
fin.close();
fin2.close();
}
}
Creation Of Custom Enumeration
Java
import java.util.Enumeration;
import java.lang.reflect.Array;
public class EnumerationClass implements Enumeration {
private int size;
private int cursor;
private final Object array;
public EnumerationClass(Object obj)
{
Class type = obj.getClass();
if (!type.isArray()) {
throw new IllegalArgumentException(
"Invalid type: " + type);
}
size = Array.getLength(obj);
array = obj;
}
public boolean hasMoreElements()
{
return (cursor < size);
}
public object nextElements()
{
return Array.get(array, cursor++);
}
}
Creation of Java Enumeration using String Array
Java
import java.util.*;
import java.io.*;
public class EnumerationExample {
public static void main(String args[])
{
// String Array Creation
String str[] = { "apple", "facebook", "google" };
// Array Enumeration Creation
ArrayEnumeration aenum = new ArrayEnumeration(str);
// usageof array enumeration
while (aenum.hasMoreElements()) {
System.out.println(aenum.nextElement());
}
}
}
Methods Of Enumeration Interface
Modifier And Type
| Method
| Explanation
|
---|
default Iterator<E> | asIterator() | This method returns an Iterator which traverses all the remaining elements covered by this enumeration. |
boolean | hasMoreElements() | On implementation, it returns the boolean value if there are more elements to extract or not and returns false when all the elements have been enumerated. |
E | nextElement() | This method returns the next element of the enumeration. It throws NoSuchElementException when there are no more elements. |
Similar Reads
Evolution of Interface in Java In Java, interfaces define a contract for classes without specifying how the methods are implemented. With time, Java has introduced major enhancements or we can say evolution of interfaces to make them more powerful. So, in this article, we will explore the evolution of interfaces in Java from Java
3 min read
Collection Interface in Java The Collection interface in Java is a core member of the Java Collections Framework located in the java.util package. It is one of the root interfaces of the Java Collection Hierarchy. The Collection interface is not directly implemented by any class. Instead, it is implemented indirectly through it
6 min read
Deque Interface in Java Deque Interface present in java.util package is a subtype of the queue interface. The Deque is related to the double-ended queue that supports adding or removing elements from either end of the data structure. It can either be used as a queue(first-in-first-out/FIFO) or as a stack(last-in-first-out/
9 min read
Externalizable interface in Java Externalization serves the purpose of custom Serialization, where we can decide what to store in stream.Externalizable interface present in java.io, is used for Externalization which extends Serializable interface. It consist of two methods which we have to override to write/read object into/from st
3 min read
Java Functional Interfaces A functional interface in Java is an interface that contains only one abstract method. Functional interfaces can have multiple default or static methods, but only one abstract method. Runnable, ActionListener, and Comparator are common examples of Java functional interfaces. From Java 8 onwards, lam
7 min read
Interfaces and Inheritance in Java Java supports inheritance and interfaces, which are important concepts for building reusable code. A class can extend another class and can implement one and more than one Java interface. Note: This topic has a major influence on the concept of Java and Multiple Inheritance. Interface Implementation
7 min read