Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Interview-Questions Enumeration and Iterator in Java
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Harjeet Singh
Enumeration and Iterator in Java
4 Answer(s)      6 years and 10 months ago
Posted in : Java Interview Questions


Difference between Enumeration and Iterator in Java programming language.


Advertisement
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
Enumeration in java
Enumeration in java In this section we will discuss about enumeration in java. Before getting details of enumeration, first we will describe about "... : Download Source Code Now, we will discuss about Enumeration in java. Enumeration
Advertisements
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
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
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
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
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
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 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
JSP Enumeration
JSP Enumeration          Enumeration, a concept of core java is an object that implements the Enumeration interface generate a series of elements, one
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
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 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
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
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
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 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 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 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
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
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
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
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
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
 

 

 

DMCA.com