AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



java-collection: Iterator vs ListIterator vs Enumerator

What are the differences between the iterator, listIterator, Enumarator.

Could you plz explain with sample code.

java x 210
collection x 52
Posted On : 2016-08-04 08:37:41.0
profile MOHAMMAD SALEEM BASHA - anyforum.in MOHAMMAD SALEEM BASHA
266150
up-rate
3
down-rate

Answers


Enumeration and Iterator are interfaces in Java for getting successive elements. Enumeration is older while iterator was introduced later with some improvements. So it is preferred to use Iterator.

Enumeration interface:
----------------------------------------------------------------
Enumeration interface has two methods:

hasMoreElements()

nextElement()


Iterator interface:
------------------------------------------------------
Iterators differ from enumerations by adding an optional remove operation, and having shorter method names.

Methods of Iterator:
-----------------------------------------------------
hasNext()

next()

remove()

Iterator is considered more secure and safe because it does not allow other thread to modify the collection object while some thread is iterating over it and throws ConcurrentModificationException in such cases.



Example:
-----------------------------------
ArrayList implements Collection interface, which extends Iterable interface:

List<String> list = new ArrayList<String>();

list.add("one");

list.add("two");

list.add("three");


Using Iterator we can Iterate the list as:

Iterator it = list.iterator();

while(it.hasNext())

{

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

}

This is because ArrayList implements Iterable and has the iterator() method available.



You can also directly use an Iterable implementation in the enhanced for loop without explicitly creating an Iterator:

for(String s:list){
System.out.println(s);
}


Iterators vs. Enumerations:
-----------------------------------------------------------------
Iterator has taken the place of Enumeration in the Java collections framework.

Iterator is considered more secure and safe.

Java Specification recommends new implementations to consider using Iterator in preference to Enumeration as the functionality of Enumeration interface is duplicated by the Iterator interface with major improvements.

Enumerations might be faster compared to Iterators due to their read only nature. Still it would be better to go with Iterators as it is the Java recommendation. StringTokenizer is one method in Java that still uses Enumeration.


ListIterator is a sub-interface of Iterator.

ListIterator allows the programmer to traverse the list in both direction, modify the list during iteration, and obtain the iterator´s current position in the list.

A ListIterator has no current element; its cursor position always lies between the element that would be returned by a call to previous() and the element that would be returned by a call to next().

The java.util.List has a listIterator() method that returns a listIterator over the elements in this list (in proper sequence).



Methods of a ListIterator are:
------------------------------------------------------------------
add(Object obj)

hasNext()

hasPrevious()

next(), returns Object

nextIndex()

previous(), returns Object

previousIndex()

remove()

set(Object obj)

Posted On : 2016-08-06 23:48:07
Satisfied : 2 Yes  0 No
profile Rishi Kumar - anyforum.in Rishi Kumar
523187021414
Reply This Thread
up-rate
5
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in