Enumeration and Iterator in Java

Enumeration and Iterator in Java

Difference between Enumeration and Iterator in Java programming language.

View Answers

February 25, 2008 at 12:45 PM

Difference between Enumeration and Iterator

Both are used to go through (traverse) into collection bag.

Enumeration
Enumeration is the part of legacy classes that deals with the objects and it comes with java1.1. Enumeration is used to go through vector, hashtable etc. Enumeration is read only and we can't change the object or value in the collection.we has only forward facility.

Iterator
Iterater is introduced in Collection framework. Iterator is used with loops like while loop, for loop etc. Iterator has an additonal method remove() that allows removal of the item from collection.


Difference between ArrayList and Vector

ArrayList

Arraylist are the new collection and are more flexible than any other collection as arraylists are dynamic in nature. Since the methods of Arraylist are not synchronized therefore they are faster in performace. Arraylist has no default size. In ArrayList data is retrived through get() method.

Vector

Vectors are the old collections therefore they comes under the legacy classes. They are synchronized. Vector has a default size of 10. Vectors are used only in those conditions multiple threads are accessed simultaneously otherwise ArrayList is the best option. Vector grows dynamically. In vector the data is retrieve through elementAt() method.


Difference between Abstract Class and Interface

Abstract Class

An abstract class can contain non-abstract methods. While a class that extends an abstract class need not implement any of the methods defined in the abstract class. The class that extends an abstract class can implement any number of it's own methods. An abstract class can hold the sharable resources. There is no need to make a class fully abstract because in such conditions they impact on performance issues

Interface

In case all the methods are set to be abstract, an interface is used. A class implementing an interface must implement all of the methods defined in the interface, interfaces do not allow to put the sharable resources. We can use an interface as a marker interface.

June 11, 2011 at 2:21 PM

The functionality of Enumeration interface is duplicated by the Iterator interface. Iterator has a remove() method while Enumeration doesn't. Enumeration acts as Read-only interface, because it has the methods only to traverse and fetch the objects, where as using Iterator we can manipulate the objects also like adding and removing the objects. I have also blogged about **Deleted** here.


June 4, 2012 at 11:21 AM

Only major difference between Enumeration and iterator is Iterator has a remove() method while Enumeration doesn't. Enumeration acts as Read-only interface, because it has the methods only to traverse and fetch the objects, where as by using Iterator we can manipulate the objects like adding and removing the objects from collection e.g. Arraylist.

Also Iterator is more secure and safe as compared to Enumeration because it does not allow other thread to modify the collection object while some thread is iterating over it and throws ConcurrentModificationException. This is by far most important fact for me for deciding between Iterator vs Enumeration in Java.


June 4, 2012 at 11:21 AM

Only major difference between Enumeration and iterator is Iterator has a remove() method while Enumeration doesn't. Enumeration acts as Read-only interface, because it has the methods only to traverse and fetch the objects, where as by using Iterator we can manipulate the objects like adding and removing the objects from collection e.g. Arraylist.

Also Iterator is more secure and safe as compared to Enumeration because it does not allow other thread to modify the collection object while some thread is iterating over it and throws ConcurrentModificationException. This is by far most important fact for me for deciding between Iterator vs Enumeration in Java.









Related Tutorials/Questions & Answers:
Java Enumeration
.style2 { background-color: #FFFFCC; } Java Enumeration       An Enumeration is an object that provides you element one at a time. This is passed
Iterator in Java Example
Iterator in Java Example We are going to discuss about iterator in java. Iterator is an interface in the collection framework and it works similar to the Enumeration. The java iterator traverse through all the elements
Advertisements
Enumeration in java
Enumeration in java In this section we will discuss about enumeration in java..._TO_REPLACE_1 Download Source Code Now, we will discuss about Enumeration in java. Enumeration is an interface in java which allows you to iterate or obtain one
Java Enumeration
Java Enumeration In this section you will learn about Enumeration interface... is telling, the Enumeration interface have functions by which you can obtain one element at a time from the collection of object. Now a days, Iterator replaces
Iterator in java
enumeration in java collection. Iterator in java, is traversing the object from...Iterator in java In this section you will learn about Iterator in java. ... are traversed based on index but Iterator in java is to traverse through object as well
Java :Thread Enumeration
Java :Thread Enumeration In this tutorial you will learn about java thread enumeration. Thread Enumeration : For enumeration, thread uses two methods activeCount() and enumerate(). activeCount() method returns count of all active
Java Iterator with Example
Iterator is an interface in the collection framework It traverses through all the elements of the collection. It works like enumeration. It has methods hasNext() and next(). Java Iterator Example import java.util.
Iterator in java, Iterator Java Examples
The Iterator is an java interface, it can be used to iterate the java collection objects. In this Java iterator tutorial you will learn how to define of Java Iterator interface
Iterator Java Tutorial
Java Iterator is an interface in the collection framework It traverses through all the elements of the collection. It works like enumeration. It has... . Example of Java Iterator import java.util.*; public class iterator1
Iterator Java Sample
Java Sample Iterator interface makes the traversing of the elements easy. It works similar to the Enumeration. It has method next() and haswNext() Java Sample Iterator Example import java.util.ArrayList; import
Java arraylist iterator
ArrayList has iterator() method.Using iterator() method  elements of the arraylist easily gets retrieved. iterator() can be used in the for loop. It is clear by the given example. Example of Java Arraylist
Java List Iterator
Java List Iterator is an interface in the collection framework It traverses through all the elements of the collection. It works like enumeration. It has methods hasNext() and next(). Example of Java List Iterator import
JSP Enumeration
JSP Enumeration          Enumeration, a concept of core java is an object that implements the Enumeration interface generate a series of elements, one
Example of Hashset iterator method in java.
Example of Hashset iterator method in java. In this exmple of HashSet class,  you will see the use of iterator() method. It is used for traversing all element from HashSet. Code:  HashSetRemoveElement.java package
Java Array Iterator with Example
Java Array Iterator is an interface in the collection framework. Java... the elements of the ArrayList can be traversed by the Iterator. There can be array of the Iterator interface to manipulate more than one ArrayList Java Array
Java Next Iterator
it should be casted for further use. Example of Java Next Iterator import... Iterator is used by the set, List Interface and its subclasses. Iterator...(); for (Integer int1 : ar) { list.add(int1); } Iterator
Java Collection iterator with example
The Java Collection Iterator is present at the highest level interface in the Collection framework. Iterator interface has methods for traversing.... Example of Java Collection Iterator import java.util.ArrayList; import
Java HashMap iterator and example
Java HashMap Iterator is an interface. It keeps the data in the key and value form. It is implemented by HashMap. hashMap doesnot have iterator method... elements can be traversed by the Iterator. Example of Java HashMap
Example to show Enumeration exception in java
Example to show Enumeration exception in java       Enumeration is mean of retaining all the series of element at a time. An object implement interface generates a series of all
How to Define Vector Iterator in Java Program
How to Define Vector Iterator in Java Program  Hi, I want to develop a small application where i want to define vector iterator in my Java Programming. Kindly Suggest any online reference for references. Thanks
Java Set iterator with example
sorted data. It uses iterator() method to traverse the data Example of Java Set Iterator import java.util.*; public class setiterator { public static... Java Set Interface keeps the data without duplicate value. Its one subtype
Java Set Iterator
data. It uses iterator() method to traverse the data Java Set Iterator..."); s.add("car"); Iterator it = s.iterator(); while (it.hasNext
Java Map iterator with example
Java Map Iterator is an interface. It keeps the data in the key and value form. It is implemented by HashMap, Tree Map. Map has no iterator method. So...() method to get the data in Set object form. Java Map Iterator with Example
Java For loop Iterator
Iterator can be used with the for loop also. Initialization of the Iterator is done before the for loop. Iterator has boolean value with the hasNext method. So no increment/decrement is required. Java for Loop Iterator
What is an Iterator?
is iterator? In this programming tutorial it explain how to use the iterator interface with examples. visit to http://www.roseindia.net/tutorial/java/iterator...What is an Iterator?  Hi, What is an Iterator? Please help me
Java Generic Iterator
Java Generic Iterator is the new feature added to the jdk1.5. It is used.... It is used with classes, methods and interfaces. Java Generics Iterator...);//gives error Iterator it = list.iterator(); while (it.hasNext
Java Hashmap Iterator
Java HashMap Iterator is a collection class. It implements the Map interface. It keeps the data in the key and value form. Java HashMap has no iterator.... Set's all elements can be traversed by the Iterator. Java Hashmap Iterator
Collection : Iterator Example
Collection : Iterator Example In this section we will discuss Iterator with example. Iterator : Iterator interface is a member of the Java Collection Framework. It takes place of Enumeration but it differ from enumeration. It is used
List iterator java example
Java List Iterator is an interface in the collection framework. List is an interface. Its all elements can be traversed by the Iterator. Java List Iterator has methods hasNext() and next() for traversing . Java List Iterator
Java Hashtable Iterator
be traversed by the Iterator. Java Hashtable Iterator Example import... in the key and value form. Like hasmap, it has no iterator() method. Use the entrySet..."); hastab.put("e", "era"); Set s = hastab.entrySet(); Iterator
Iterator Java Order
. The iterator() method of the List Interface gives elements in propersequence. The iterator() method of the Set Interface gives elements inimproper sequence. Java Order Iterator Example import java.util.ArrayList; import
Java Hasnext Iterator
Java HasNext Iterator is the method of the Iterator Interface. It returns... of false vale loop terminates. Java HasNext Iterator Example import... : months) { list.add(m); } Iterator iterator = list.iterator
Java Vector Iterator
Java Vector Iterator is a Collection class. It has similar functionality... increase or decrease. It has iterator() method. So Iterator interface can traverse all its elements. Java Vector Iterator Example import java.util.Iterator
Iterator Java While
The Java While loop Iterator is the top tested loop. It is in two forms while(), do while() While loop is mostly used with Iterator compared to for loop Java While Loop Iterator Example import java.util.ArrayList; import
Vector Iterator Java Example
growable array.Its size may increase or decrease. It has iterator() method.So Iterator interface can traverse all its elements. Java Vector Iterator with Example... : city) { v.add(s); } Iterator it = v.iterator(); for (; it.hasNext
Iterator Java Size
The Java Iterator size() is the method of the collection Interface... in the collection. Java Size Iterator Example import java.util...." }; for (int i = 0; i < 5; i++) { list.add(country[i]); } Iterator i
Iterator Java Remove
with both Iterator and listIterator. It is used with next() or previous() method. Example Java Remove Iterator import java.util.ArrayList; import...++); } Iterator it = list.iterator(); while (it.hasNext
Example to show Iterator exception in java
Example to show Iterator exception in java       Here we are describing the use of using exception class in java .This tutorial describes the way to handle Iterator
ModuleNotFoundError: No module named 'python-enumeration'
ModuleNotFoundError: No module named 'python-enumeration'  Hi, My... named 'python-enumeration' How to remove the ModuleNotFoundError: No module named 'python-enumeration' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'python-enumeration'
ModuleNotFoundError: No module named 'python-enumeration'  Hi, My... named 'python-enumeration' How to remove the ModuleNotFoundError: No module named 'python-enumeration' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'django-enumeration'
ModuleNotFoundError: No module named 'django-enumeration'  Hi, My... named 'django-enumeration' How to remove the ModuleNotFoundError: No module named 'django-enumeration' error? Thanks   Hi, In your
Iterator tag example of struts2.2.1
Iterator tag example of struts2.2.1 In this tutorial, we will introduce you to about the iterator tag. Iterator tag is used to iterate over a value... the iterator tag to iterator over the collection of Integers. Here the integer
ModuleNotFoundError: No module named 'iterator'
ModuleNotFoundError: No module named 'iterator'  Hi, My Python... 'iterator' How to remove the ModuleNotFoundError: No module named 'iterator... to install padas library. You can install iterator python with following
iterator display problem - Struts
iterator display problem   in action class i store database data... iterator tag can i use id atribute or valuethat i not understand  Hi friend, Code to help in solving the problem : Iterator Tag
J2ME Enumeration Example
J2ME Enumeration Example       Enumeration is an Interface that comes with the java.util package... will be able to run a video file using interface enumeration in J2ME. Interface
ModuleNotFoundError: No module named 'concurrent-iterator'
ModuleNotFoundError: No module named 'concurrent-iterator'  Hi, My... named 'concurrent-iterator' How to remove the ModuleNotFoundError: No module named 'concurrent-iterator' error? Thanks   Hi
ModuleNotFoundError: No module named 'list_iterator'
ModuleNotFoundError: No module named 'list_iterator'  Hi, My... 'list_iterator' How to remove the ModuleNotFoundError: No module named 'list_iterator' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'osm-iterator'
ModuleNotFoundError: No module named 'osm-iterator'  Hi, My Python... 'osm-iterator' How to remove the ModuleNotFoundError: No module named 'osm-iterator' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'bounded-iterator'
ModuleNotFoundError: No module named 'bounded-iterator'  Hi, My... named 'bounded-iterator' How to remove the ModuleNotFoundError: No module named 'bounded-iterator' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'concurrent-iterator'
ModuleNotFoundError: No module named 'concurrent-iterator'  Hi, My... named 'concurrent-iterator' How to remove the ModuleNotFoundError: No module named 'concurrent-iterator' error? Thanks   Hi

Ads