Why doesn't Iterator add a method, whereas List Iterator has one in Java?

Can anyone explain why it looks like this.

Iterator doesn't have add () method, but List Iterator has one in Java.

+3


source to share


8 answers


An object Iterable

can often be immutable. The more specialized one Iterator

, who knows that the underlying object supports adding items, can implement this functionality. As with the ListIterator

.



To be fair, @Eran's answer makes more sense.

+1


source


My initial thought was, like some of the other answers, to say what Iterator

can be implemented by an immutable class. However, this answer is not enough, since Iterator has a method delete

(although it does not have to be supported in all implementations).



I think the best reason for not having insert

in Iterator

is that it would only matter in ordered data structures (such as lists). For example, it doesn't make sense to add an element to Set

via an iterator of this Set

.

+8


source


Lists differ from Iterable collections in general in that the well-defined ordinal position of all elements in the list and the well-defined ordinal position in the list of any iterator over it are available / known.

As a consequence, intelligently defining an insert operation using an iterator is possible if and only if what is being repeated is such a list.

The same is not true, for example, for HashSets and TreeSets. With such collections, it is generally not predicted whether the insertion will be "before" or "after" the current iterator position. So in general it is not predicted whether the iterator will operate on elements that were inserted during iteration or not. Thus, such a feature could cause, in a sense, erratic / random / unpredictable behavior. I would not include the insert () function if I had to create a language.

+1


source


ListIterator allows you to add an item after an item it has recently read. Since adding an item to the list is less expensive (since it allows duplicates), adding is allowed. The iterator doesn't need to move the list back and forth when inserting into the list. This might be the reason why traversing with the ListIterator allows you to add an item. Also the extension for this logic is applicable to the delete option, since to delete we need to go through the entire collection before deleting the item.

0


source


http://www.google.com/search?q=java+iterator+insert+rationale :

Java Collections APIs FAQ - Oracle Documentation http://docs.oracle.com/javase/8/docs/technotes/guides/collections/designfaq.html :

Oracle Technology Network
Software Downloads
Documentation

      

Java Collections APIs FAQ

Collection interface

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

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

They mean that it is not clear what the semantics should be for unordered iterations.

There are reasonable sampling of semantics for arbitrary unordered iterations if performance is not a concern, for example, the added element either won't, or will appear, or may appear on further iteration. But with each of these options, allowing for iteration and insertion involves hitting performance versus iteration-only behavior. Performance matters, so there is no wise choice of semantics.

0


source


In addition to the ordering argument, which is supported by Oracle Java Collections API FAQ, I thought about the following argument.

Iterator

was designed to iterate over all kinds of collections in Java. As suggested by others, collection Set

does not allow duplicate entries, or alternatively, some collections (for example BlockingQueue

) do not allow value null

.

Adding operations add()

and set()

to Iterator

can potentially lead to an operation that is not supported by the underlying base (however, unlike, remove()

it will depend on the passed value).

On the other hand, ListIterator

these techniques have been developed to deal with this situation. They both would throw IllegalArgumentException

if some aspect of the element prevents it from being added to the list.

0


source


  • The iterator will be used to iterate through set / list / queue. All of these collections have their own ordering logic. For example, for example "TreeSet" is a sorted data structure. Now, iterating through the treeSet let {2,5,7,19,22}; when the cursor is 7, the programmer tries to add 29. in this scenario, the trees will be sorted and damaged. the method should not be allowed.

  • But in remove () we only remove an element from a specific object position, which can never harm the order. So it's allowed.

  • Go to ListIterator which add () allows. Remember that it is only used for a List, which maintains the order of its elements according to the insert
    order. Therefore, if we add an element at any given point, the order of the algorithm will not be difficult.

0


source


Iterator

is intended for iteration, not adding objects.

List

, on the other hand, is iterable (which is why it has Iterator

), but is also capable of manipulating objects, therefore add

.

See definition Iterable

and List

to find out what their purpose is.

-1


source







All Articles