Compare the Difference Between Similar Terms

Difference Between

Home / Technology / IT / Programming / Difference Between Enumeration and Iterator

Difference Between Enumeration and Iterator

June 13, 2011 Posted by Indika

Enumeration vs Iterator

There are many data structures that act as collections in Java such as Vectors, Hash tables and classes that implements Java Collections Framework (i.e. HashMap, HashSet, ArrayList, TreeSet, TreeMap, LinkedList, LinkedHashMap and LinkedHashSet). There are numerous ways to iterate through the individual elements of the objects in Java. Java provides two interfaces to make this task easier. Enumeration and Iterator are two of the interfaces found in java.util package that provide functionality to enumerate through sequences or objects with a set of items. Enumerator was introduced in JDK 1.0 and Iterator which was introduced in JDK 1.2 virtually duplicates the functionality of the Enumerator (within the Collections Framework).

What is Enumeration?

Enumeration is a public interface in Java, introduced in JDK 1.0, which provides the ability to enumerate through sequences of elements. It is found under java.util package. When the Enumeration interface is implemented by an object, that object can generate a sequence of elements. Enumeration interface has two methods. The method hasMoreElements() will test if this enumeration contains more elements and the nextElement() returns the next element in the sequence (if there is at least one more to go). In other words, by calling nextElement() successively, the programmer can access the individual elements in the series. For example, to print all elements in Vector v1 using Enumerator, the following code snippet can be used.

Enumeration e = v1.elements();

While(e.hasMoreLements()){

System.out.println(e.nextElement());

}

Enumerator can also be used to define the stream of input to the SequenceInputStream objects.

What is Iterator?

Iterator is a public interface in Java.util package, which allows iterating through elements of the collections objects that implement the Collections framework (such as ArrayList, LinkedList, etc.). This was introduced in JDK 1.2 and replaced the Enumerator within the Java Collections Framework. Iterator has three methods. The method hasNext() tests whether there are remaining elements in the collection and the next() method returns the next element in the series. The remove() method can be used to remove the current element from the underlying collection. For example, to print all elements in Vector v1 using Iterator, the following code snippet can be used.

Iterator i = v1.elements();

While(i.hasNext()){

System.out.println(e.next());

}

What is the difference between Enumeration and Iterator?

Although, Enumeration and Iterator are two of the interfaces found in java.util package, which allow iterating/enumerating through elements of a series, they have their differences. Actually, Iterator, which was introduced after Enumeration, replaces the Enumeration within the Java Collections framework. Unlike Enumeration, Iterator is fail-safe. This means that concurrent modifications (to the underlying collection) are not allowed when Iterator is used. This is very useful in multi-threaded environments where there is always a risk of concurrent modifications. In the event of a concurrent modification, the Iterator object will throw a ConcurrentModificationException. Iterator has shorter method names compared to Enumerator. Furthermore, iterator has the additional functionality of deleting elements during the iteration (which is not possible using Enumerator). So, if there is a need to remove elements from the collection, Iterator is the only option that can be considered.

Related posts:

Difference between Java and J2EE Difference Between Java and C language Difference Between Procedures and Functions in Programming Difference Between Hashtable and Hashmap Difference Between Virtual and Abstract

Filed Under: Programming Tagged With: Enumeration, hasMoreElements(), hasNext(), Iterator, Java, Java Collections Framework, Java interfaces, java.util package, JDK 1.0, JDK 1.2, next(), nextElement(), public interface in Java, remove(), three methods of iterator

About the Author: Indika

Indika, BSc.Eng, MSECE Computer Engineering, PhD. Computer Science, is an Assistant Professor and has research interests in the areas of Bioinformatics, Computational Biology, and Biomedical Natural Language Processing.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Request Article

Featured Posts

Difference Between Coronavirus and Cold Symptoms

Difference Between Coronavirus and Cold Symptoms

Difference Between Coronavirus and SARS

Difference Between Coronavirus and SARS

Difference Between Coronavirus and Influenza

Difference Between Coronavirus and Influenza

Difference Between Coronavirus and Covid 19

Difference Between Coronavirus and Covid 19

You May Like

Difference Between Philosophy and Ideology

Difference Between Agar and Alginate

Difference Between Agar and Alginate

Difference Between Experimental and Observational Study

Difference Between Experimental and Observational Study

Difference Between Bivalent and Synaptonemal Complex

Difference Between Bivalent and Synaptonemal Complex

Difference Between Issue and Concern

Difference Between Issue and Concern

Latest Posts

  • What is the Difference Between Lip Flip and Lip Filler
  • What is the Difference Between Bone Spurs and Plantar Fasciitis
  • What is the Difference Between Tension Pneumothorax and Cardiac Tamponade
  • What is the Difference Between Diverticulitis and Crohn’s Disease
  • What is the Difference Between Sharara and Lehenga
  • What is the Difference Between Leucoderma and Albinism
  • Home
  • Vacancies
  • About
  • Request Article
  • Contact Us

Copyright © 2010-2018 Difference Between. All rights reserved. Terms of Use and Privacy Policy: Legal.