public final class CoIterator<E> extends Object
This is an iterator that walks a pair of collections, returning matching pairs from the set.
When an element is present in the left set but there is no equal object in the right set, the pair (leftobj, null) is returned.
When an element is present in the right set but there is no equal object in the left set, the pair (null, rightobj) is returned.
When an element in one set has an equal element in the other set, the pair (leftobj, rightobj) is returned.
Note that the phrase "pair is returned" above actually means that the getLeft and getRight methods on the iterator return those objects; the pair is "conceptual" rather than a physical Pair instance. This avoids instantiating an object to represent the pair for each step of the iterator which would not be efficient.
Note also that elements from the sets are always returned in the iteration order.
Constructor and Description |
---|
CoIterator(Iterator<? extends E> left,
Iterator<? extends E> right)
Assumes the iterators iterate over comparable elements and uses their natural ordering instead of an explicit
comparator.
|
CoIterator(Iterator<? extends E> left,
Iterator<? extends E> right,
Comparator<? super E> comparator)
The iterators must iterate over sorted collections otherwise this instance might not
produce the intended results.
|
Modifier and Type | Method and Description |
---|---|
E |
getLeft()
After calling
next() , this will contain the next element from the "left" collection. |
E |
getRight()
After calling
next() , this will contain the next element from the "right" collection. |
boolean |
hasNext() |
void |
next()
Use
getLeft() and getRight() to get the next elements from the two iterated collections. |
public CoIterator(Iterator<? extends E> left, Iterator<? extends E> right, Comparator<? super E> comparator)
Also, the iterators must not ever return null - i.e. the collections must not contain null values otherwise the behavior of the iteration is undefined.
left
- the iterator over "left" collectionright
- the iterator over "right" collectioncomparator
- the comparator used to sort the collections (this must have been done prior to calling this
constructor)public CoIterator(Iterator<? extends E> left, Iterator<? extends E> right)
If E
is not at the same time comparable, calling next()
will fail with a class cast
exception at the first mutual comparison of elements from the two collections.
left
- the iterator over the "left" collectionright
- the iterator over the "right" collectionCoIterator(java.util.Iterator, java.util.Iterator, java.util.Comparator)
public boolean hasNext()
public void next()
getLeft()
and getRight()
to get the next elements from the two iterated collections.public E getLeft()
next()
, this will contain the next element from the "left" collection.Copyright © 2014-2021 Lukas Krejci. All Rights Reserved.