why there is no add method in Iterator interface

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


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.