Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

In this tutorial, we’ll learn about Enumeration and Iterator in Java. We’ll also learn how to use them in code and what are the differences between them.

2. Introduction to Enumeration and Iterator

In this section, we’ll learn about Enumeration and Iterator conceptually and their use.

2.1. Enumeration

Enumeration has been present in Java since version 1.0. It’s an interface and any implementation allows one to access elements one by one. In simple terms, it’s used to iterate over a collection of objects such as Vector and Hashtable.

Let’s look at an example for Enumeration:

Vector<Person> people = new Vector<>(getPersons());
Enumeration<Person> enumeration = people.elements();
while (enumeration.hasMoreElements()) {
    System.out.println("First Name = " + enumeration.nextElement().getFirstName());
}

Here, we’re printing firstName of a Person using Enumeration. The elements() method provides a reference to Enumeration and by using that we can access elements one by one.

2.2. Iterator

Iterator has been present in Java since 1.2 and is used to iterate over collections that were also introduced in the same version.

Next, let’s print firstName of a Person using Iterator. iterator() provides a reference to Iterator and by using that we can access elements one by one:

List<Person> persons = getPersons();
Iterator<Person> iterator = persons.iterator();
while (iterator.hasNext()) {
    System.out.println("First Name = " + iterator.next().getFirstName());
}

So, we can see Enumeration and Iterator are both present in Java since 1.0 and 1.2 respectively, and are used to iterate over a collection of objects one at a time.

3. Difference Between Enumeration and Iterator

In this table, we’ll understand the differences between Enumeration and Iterator:

Enumeration Iterator
Present since Java 1.0 to enumerate Vectors and Hashtables Present since Java 1.2 to iterate over Collections such as List, Set, Map, etc
Contains two methods: hasMoreElements() and nextElement() Contains three methods: hasNext(), next() and remove()
Methods have long names Methods have short and concise names
Have no methods to remove elements while iterating Have to remove() to remove elements while iterating
asIterator() added in Java 9 gives Iterator on the top of Enumeration. However, remove() in this particular case throws UnsupportedOperationException forEachRemaining() added in Java 8 performs actions on the remaining elements

4. Conclusion

In this article, we’ve understood Enumeration and Iterator, how to use them using code examples, and the various differences between them.

All the code example used in this article is available over on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.