Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why there is no add method in Iterator interface

Tags:

In Iterator Sun added the remove method to remove the last accessed element of the collection. Why there is no add method to add a new element to the collection? What kind of side-effects it may have to the collection or iterator?

like image 718
droidsites Avatar asked Jun 25 '12 20:06

droidsites


People also ask

Why list iterator has add () method and iterator does not?

Iterator is for iterating, not adding objects. List on the other hand is iterable (therefore it has Iterator ) but also it is able to manage entities, therefore the add .

Can we add element in iterator?

No way to add new elements to the collection. You can add new elements to the collection. Iterator cannot modify the elements during iteration. ListIterator can modify the elements in the collection using the set() method.

Why ListIterator has add () method?

Java ListIterator add() Method The add() method of ListIterator interface is used to insert the given element into the specified list. The element is inserted automatically before the next element may return by next() method.


2 Answers

Okay, here we go:

The answer is clearly stated in the design faq:

Why don't you provide an Iterator.add method?

The semantics are unclear, given that the contract for Iterator makes no guarantees about the order of iteration. Note, however, that ListIterator does provide an add operation, as it does guarantee the order of the iteration.

http://docs.oracle.com/javase/1.4.2/docs/guide/collections/designfaq.html#10

like image 97
jontro Avatar answered Oct 01 '22 04:10

jontro


The sole purpose of an Iterator is to enumerate through a collection. All collections contain the add() method to serve your purpose. There would be no point in adding to an Iterator because the collection may or may not be ordered (in the case of a HashSet).

EDIT: While working on another problem, I came up with another reason that Iterator lacks an add() method. Looking under the hood of ArrayList (line 111), and HashMap (line 149), we see that the implementation is just a few methods surrounding an array of objects. Now we consider how arrays are treated in memory.

zero-based array indexes

This is an array of 5 elements. However, there are six indices. The letter "a" in this array is listed as element 0 because in order to read it, left to right like a computer does, you have to start at index 0. Now, if we are iterating through this array (yes, collection, but it boils down to an array), we will start at index 0 and continue to index 1. At this point in the Iterator, we want to call add("f");. At this point, let's compare the implications of add() and remove(). remove() would leave a space in the array, which is easy to jump over, because we can immediately recognize that it isn't a member. On the other hand, add() would put a new element in which wasn't there before. This will affect the length of the array that we're iterating through. What happens when we get to that last element? Can we even guarantee that it is there (that is, that the array hasn't exceeded the maximum size)?

All in all, the arguments one way or another both have valid points, but the bottom line is that the behavior of an add() method is not well defined in all cases. Sun had to make a choice where to limit functionality, and they chose not to include this method.

like image 25
gobernador Avatar answered Oct 01 '22 04:10

gobernador