Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there a merged iterator implementation?

Tags:

java

iterator

Is there an Iterator implementation that merges multiple iterators?

class MergedIterator<T> implements Iterator<T>
{
     MergedIterator(Iterator<T>... iters)
     ....
}

And the next method should move on to iters[1] when !iters[0].hasNext() etc

like image 566
Miserable Variable Avatar asked Apr 18 '26 16:04

Miserable Variable


2 Answers

I'd call that a ConcatenatedIterator myself - a MergedIterator should merge the results of several iterators e.g. based on sorting

Naming aside, I'm sure there'll be an implementation in a 3rd party library somewhere. Just off to check Google collections...

EDIT: Bingo - Iterators.concat

like image 155
Jon Skeet Avatar answered Apr 21 '26 07:04

Jon Skeet


Commons Collections IteratorChain

like image 29
JodaStephen Avatar answered Apr 21 '26 06:04

JodaStephen