Iterator for maps of type Object and int.
The iterator semantics for Trove's primitive
maps is slightly different from those defined in java.util.Iterator, but still well
within the scope of the pattern, as defined by Gamma, et al.
This iterator does
not implicitly advance to the next entry when the value at the current position is
retrieved. Rather, you must explicitly ask the iterator to advance() and then retrieve
either the key(), the value() or both. This is done so that you have the
option, but not the obligation, to retrieve keys and/or values as your application requires, and
without introducing wrapper objects that would carry both. As the iteration is stateful, access
to the key/value parts of the current map entry happens in constant time.
In
practice, the iterator is akin to a "search finger" that you move from position to position. Read
or write operations affect the current entry only and do not assume responsibility for moving the
finger.
Here are some sample scenarios for this class of iterator:
// accessing keys/values through an iterator:
for (TObjectIntIterator it = map.iterator();
it.hasNext();) {
it.advance();
if (satisfiesCondition(it.key()) {
doSomethingWithValue(it.value());
}
}
// modifying values in-place through iteration:
for (TObjectIntIterator it = map.iterator();
it.hasNext();) {
it.advance();
if (satisfiesCondition(it.key()) {
it.setValue(newValueForKey(it.key()));
}
}
// deleting entries during iteration:
for (TObjectIntIterator it = map.iterator();
it.hasNext();) {
it.advance();
if (satisfiesCondition(it.key()) {
it.remove();
}
}
// faster iteration by avoiding hasNext():
TObjectIntIterator iterator = map.iterator();
for (int i = map.size(); i-- > 0;) {
iterator.advance();
doSomethingWithKeyAndValue(iterator.key(), iterator.value());
}