MaatDeamon
MaatDeamon

Reputation: 9761

Is there any benefit of working with an Iterator over a List

Is there any benefit of manipulating an Iterator over or List ?

I need to know if concatenating 2 iterators is better that concatenating to List ?

In a sense what the fundamental difference between working with iterator over the actual collection.

Upvotes: 2

Views: 159

Answers (1)

ubadub
ubadub

Reputation: 3880

An Iterator isn't an actual data structure, although it behaves similar to one. It is just a traversal pointer to some actual data structure. Thus, unlike in an actual data structure, an Iterator can't "go back," that is, access old elements. Once you've gone through an Iterator, you're done.

What's cool about Iterator is that you can give it a map, filter, or other transformation elements, and instead of actually modifying any existing data structure, it will instead apply the transformation the next time you ask for an element.

"Concatenating" two Iterators creates a new Iterator that wraps both of them.

On the other hand, Lists are actual collections and can be re-traversed.

Upvotes: 4

Related Questions