Interface WiredIterator<E>

Type Parameters:
E - The type of the elements being iterated over
All Superinterfaces:
Iterator<E>

public sealed interface WiredIterator<E> extends Iterator<E>
A one-way-only iterator that, in practice, still provides the same functionality as ListIterator. A WiredIterator lets you flip the direction of the iteration through the turn() method. Where applicable, all operations are relative to the direction of the traversal. This includes operations like next(), insertBefore() and insertAfter(). So, for example, with the direction reversed, a call to next() takes you closer to the beginning of the list and further away from the end. A WiredIterator does not keep track of the index of the current element, as does a ListIterator. This makes it less vulnerable to concurrent modifications, as it only cares about the element directly ahead of it.
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Returns the index of the current element.
    void
    insertAfter(E value)
    Inserts a new element just after the current element.
    void
    insertBefore(E value)
    Inserts a new element just before the current element.
    Returns the value that would be returned by a call to Iterator.next() without actually moving towards the next element.
    void
    Removes the current element from the underlying list.
    void
    set(E newVal)
    Sets the value of the current element.
    Flips the direction of the iteration.
    Returns the value of the current element.

    Methods inherited from interface java.util.Iterator

    forEachRemaining, hasNext, next
  • Method Details

    • set

      void set(E newVal)
      Sets the value of the current element. An IllegalStateException is thrown if Iterator.next() has not been called yet.
      Parameters:
      newVal - The new value for the element.
    • value

      E value()
      Returns the value of the current element.
      Returns:
      the value of the current element
    • peek

      E peek()
      Returns the value that would be returned by a call to Iterator.next() without actually moving towards the next element. A NoSuchElementException is thrown if the iterator has arrived at the last element.
      Returns:
      The value that would be returned by a call tonext().
    • insertBefore

      void insertBefore(E value)
      Inserts a new element just before the current element. Note that the list must contain at least one element to begin with.
      Parameters:
      value - the value to insert
    • insertAfter

      void insertAfter(E value)
      Inserts a new element just after the current element. A subsequent call to Iterator.next() would make the new element the current element. Note that the list must contain at least one element to begin with.
      Parameters:
      value - the value to insert
    • remove

      void remove()
      Removes the current element from the underlying list. Contrary to the specification for Iterator, this method multiple times in a row. Before removing the element, the WiredIterator will move back to the preceding element. Thus, after the removal, the preceding element is the current element again, and a call to next() will make the element after the removed element the current element.
      Specified by:
      remove in interface Iterator<E>
    • index

      int index()
      Returns the index of the current element. You must have called next() at least once before you can call this method. The index is calculated on demand by starting from the first element of the list and advancing until the current element is encountered. Thus, for large lists this method is very inefficient compared to the equivalent methods in ListIterator.
      Returns:
      the index of the current element
    • turn

      WiredIterator<E> turn()
      Flips the direction of the iteration. The returned Iterator is initialized to be at the same element as this Iterator. You must have called Iterator.next() at least once before you can call turn(). An IllegalStateException is thrown if Iterator.next() has not been called yet.
      Returns:
      A WiredIterator that the traverses the list in the opposite direction.