As per my knowledge the main difference between Iterator and ListIterator is
Iterator : Enables you to cycle through a collection in the forward direction only, for 开发者_如何学编程obtaining or removing elements
ListIterator : It extends Iterator, allow bidirectional traversal of list and the modification of elements
If ListIterator is more powerful than Iterator then sun java developer should have provide implementation for ListIterator only and deprecate iterator. Why Iterator is still exists in java? Is there any advantage of using Iterator than ListIterator?
It's because not all collections support both forward and backward iteration. ListIterators
are specifically for collections that have list semantics, i.e. they define an ordering over the elements. Some collection types (Sets
, for example) do not define the ordering of their elements, so a ListIterator
does not make sense for them.
There is also an extra overhead incurred when the iterator implementation needs to maintain enough state to support forward and backward iteration and in-place modification. By supporting both Iterator
and ListIterator
it is possible to have a light-weight implementation of Iterator
when it is needed, and a heavier ListIterator
for those cases where the extra functionality is required.
Just because something can do more, doesn't make it the right tool for the job.
In the case of Iterator
vs. ListIterator
, not every collection needs support for bidirectional iteration. Also, the amount of mutating power that the ListIterator
has is not necessarily appropriate for general iteration. Lastly, ListIterator
provides ways of accessing the index of an element, but many collections do not have a notion of an index. So you could say that the ListIterator
is too powerful for most collections. In fact, some people may already consider Iterator
to be "too powerful", as it provides a remove
method, which is not always appropriate either.
The basic guiding factor here is the "List" part of ListIterator
; while Iterator
should be useful for all collections, ListIterator
is specifically intended for collections which, like lists, have a well-defined linear ordering of their elements.
Some examples of where ListIterator
would be useful:
- Singly & Doubly Linked Lists
- Array Lists
- Other collections with a well-defined linear ordering
Some examples of where ListIterator
is not suited:
- Trees (many of them, anyways)
- Maps
- Sets
- Other collection which don't have a linear ordering
The power of the ListIterator comes at a cost (implementation) which would be non-trivial for non-list-style collection types like hash maps, sets etc. That's why it is only required for index-accessible collections in the Java collection framework. Also, Iterator is the underlying device in Java's foreach kind of loop, and it makes sense to back such language features up with something simple, so that it can be adapted for a wider range of types more easily.
It is because Java is backwards-binary-compatible by design and intent, so nothing has ever been removed from it, apart from the AWT event migration of 1.0->1.1.
精彩评论