NOTE: This article is 3 years or older so its information may no longer be relevant. Read on at your own discretion! Comments for this article have automatically been locked, refer to the FAQ
for more details.
are great. They provide an easy way of traversing a data structure without having to use a counter loop and without having to worry about how the data structure itself is implemented, you simply check if the Iterator
has a next
value and then get that value. Easy!
Now what if you had multiple Iterators
and you wanted to process data from all of them? An example of this is if you have a number of Threads
) that retrieve data from the database and then return an Iterator
to the processed data. That becomes a little bit more challenging.
You'd have to have a way of combining these Iterators
somehow...or chaining them to look like one single Iterator
class from Apache Commons
does exactly this!
implements the Iterator
interface, you can use it in place of any other standard Iterator
. To use IteratorChain
, either initialise it with a Collection
objects or call the addIterator()
method to add all your Iterators
to the chain.
The caveat is all your Iterators
have to return the same data type. However if that is already the case, IteratorChain
saves a good amount of work.
A quick disclaimer...
Although I put in a great effort into researching all the topics I cover, mistakes can happen.
If you spot something out of place, please do let me know.
All content and opinions expressed on this Blog are my own and do not represent the opinions of my employer (Oracle).
Use of any information contained in this blog post/article is subject to this disclaimer
Other posts you may like...