The Interface Iterator provider as three method that are hasNext(), next() and remove(). This means that we can remove any element from a collection while iterating over it. so why we cannot add element to the collection. Why they don’t provide add() method.

Answer is, that they can’t allow you to concurrently read and add element to a collection. The Iterator work on any collection whether it Set, List or Map. So it does know very much the on which underlying it’s going to work and we know all the collection implementation maintain ordering of elements based on some algorithm. For example TreeSet maintains the order of elements in by implementation Red-Black Tree data structure. Therefore if we tried to add an element to TreeSet using the iterator at given index or position of iterator. it might corrupt the state of the underlying data structure. So, the add() method could change structural state of a data structure.

Iterator is at this position we try add ‘0’ value at this position then it will break the logic of sorted tree and be no long sorted

While remove() method doesn’t change or violate any state and rule of data structure or of the algorithm.

ListIterator provide the add() methods because it know the location where it needs to add the newly created element as List preserves the order of its element in order of their insertion.

Please do comment…

Share :

Subscribe & Social

Subscribe to this blog via RSS.

Recent Posts


Java 13

Python 2

Wordpress 1

Ops 3

Shared 3

Angular 1

Web 1

Popular Tags

Java (13) Python (2) Wordpress (1) Ops (3) Angular (1) Web (1)


DGStack is web blog by digvijay bhakuni. This blog is about computer programming tips and trick. And some short tutorial and gettings started post about the framework, libriay, api and much more. The blog target mostly open source stuff like java, j2ee, nodeJs, angularJs, spring, struts, mongodb, mean, etc


This website claims no credit for any images posted on this site unless otherwise noted. Images on this blog are copyright to its respectful owners. If there is an image appearing on this blog that belongs to you and do not wish for it appear on this site, please E-mail with a link to said image and it will be promptly removed. And the information is best of my knowledge one can always write comment or mail regarding information is this blog.