Class THashIterator<V>

java.lang.Object
gnu.trove.impl.hash.THashIterator<V>
All Implemented Interfaces:
TIterator, Iterator<V>
Direct Known Subclasses:
TObjectHashIterator

public abstract class THashIterator<V> extends Object implements TIterator, Iterator<V>

Implements all iterator functions for the hashed object set. Subclasses may override objectAtIndex to vary the object returned by calls to next() (e.g. for values, and Map.Entry objects).

Note that iteration is fastest if you forego the calls to hasNext in favor of checking the size of the structure yourself and then call next() that many times:

 Iterator i = collection.iterator();
 for (int size = collection.size(); size-- > 0;) {
   Object o = i.next();
 }
 

You may, of course, use the hasNext(), next() idiom too if you aren't in a performance critical spot.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected int
    the number of elements this iterator believes are in the data structure it accesses.
    protected final THash
    the data structure this iterator traverses
    protected int
    the index used for iteration.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    Create an instance of THashIterator over the values of the TObjectHash
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Returns true if the iterator can be advanced past its current location.
    protected final void
    Sets the internal index so that the `next' object can be returned.
    Moves the iterator to the next Object and returns it.
    protected final int
    Returns the index of the next value in the data structure or a negative value if the iterator is exhausted.
    protected abstract V
    objectAtIndex(int index)
    Returns the object at the specified index.
    void
    Removes the last entry returned by the iterator.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface java.util.Iterator

    forEachRemaining
  • Field Details

    • _hash

      protected final THash _hash
      the data structure this iterator traverses
    • _expectedSize

      protected int _expectedSize
      the number of elements this iterator believes are in the data structure it accesses.
    • _index

      protected int _index
      the index used for iteration.
  • Constructor Details

    • THashIterator

      protected THashIterator(TObjectHash<V> hash)
      Create an instance of THashIterator over the values of the TObjectHash
      Parameters:
      hash - the object
  • Method Details

    • next

      public V next()
      Moves the iterator to the next Object and returns it.
      Specified by:
      next in interface Iterator<V>
      Returns:
      an Object value
      Throws:
      ConcurrentModificationException - if the structure was changed using a method that isn't on this iterator.
      NoSuchElementException - if this is called on an exhausted iterator.
    • hasNext

      public boolean hasNext()
      Returns true if the iterator can be advanced past its current location.
      Specified by:
      hasNext in interface Iterator<V>
      Specified by:
      hasNext in interface TIterator
      Returns:
      a boolean value
    • remove

      public void remove()
      Removes the last entry returned by the iterator. Invoking this method more than once for a single entry will leave the underlying data structure in a confused state.
      Specified by:
      remove in interface Iterator<V>
      Specified by:
      remove in interface TIterator
    • moveToNextIndex

      protected final void moveToNextIndex()
      Sets the internal index so that the `next' object can be returned.
    • nextIndex

      protected final int nextIndex()
      Returns the index of the next value in the data structure or a negative value if the iterator is exhausted.
      Returns:
      an int value
      Throws:
      ConcurrentModificationException - if the underlying collection's size has been modified since the iterator was created.
    • objectAtIndex

      protected abstract V objectAtIndex(int index)
      Returns the object at the specified index. Subclasses should implement this to return the appropriate object for the given index.
      Parameters:
      index - the index of the value to return.
      Returns:
      an Object value