I have an API which connects to DB and returns an Iterator. I have to call this API in loop to get records for multiple inputs. My requirement is to concatenate all iterators for which I am using com.google.common.collect.Iterators
class.
My code snippet is shown below:
Iterator<Record> iterator = Iterators.emptyIterator();
for (QueryHolder<String> queryHolder : queries) {
Iterators.concat(iterator, fetchAnotherRecordIterator(queryHolder));
}
return iterator;
But at the end I am getting empty iterator. On debugging, I am getting records from fetchAnotherRecordIterator
method.
Don't know if we can append iterators to an empty iterator.
com.google.common.collect.Iterators
is returning a new Iterator
when you call the method concat
. This means you have to update a reference with the new Iterator:
Iterator<Record> iterator = Iterators.emptyIterator();
for (QueryHolder<String> queryHolder : queries) {
iterator = Iterators.concat(iterator, fetchAnotherRecordIterator(queryHolder));
}
return iterator;