Class OrderedPairIterator<T extends @Nullable Object>

java.lang.Object
org.plumelib.util.OrderedPairIterator<T>
Type Parameters:
T - the element type of each component iterator; this OrderedPairIterator has elements of type MPair<T,T>
All Implemented Interfaces:
Iterator<MPair<@Nullable T,@Nullable T>>

public class OrderedPairIterator<T extends @Nullable Object> extends Object implements Iterator<MPair<@Nullable T,@Nullable T>>
Given two sorted iterators, this class returns a new iterator that pairs equal elements of the inputs, according to the sort order or the given comparator. If an element has no equal element in the other iterator, then the element is paired with null.

For example, suppose that the inputs are

   [1, 2, 3, 5] and
   [1, 3, 5, 7, 9].
 

Then the output is

   [(1,1), (2,null), (3,3), (5,5), (null,7), (null, 9)].
 

(This operation is similar to, but not the same as, the operation called "zipping".)

In some cases this is just the right abstraction. But in some cases it's appropriate to use set intersection/difference instead.

  • Constructor Details

    • OrderedPairIterator

      public OrderedPairIterator(Iterator<T> itor1, Iterator<T> itor2)
      Create an iterator that returns pairs, where each pair contains has an element from each iterator and the two elements are equal.
      Parameters:
      itor1 - iterator for first elements of pairs
      itor2 - iterator for second elements of pairs
    • OrderedPairIterator

      public OrderedPairIterator(Iterator<T> itor1, Iterator<T> itor2, Comparator<T> comparator)
      Create an iterator that returns pairs, where each pair contains has an element from each iterator and the two elements are equal according to the comparator.
      Parameters:
      itor1 - iterator for first elements of pairs
      itor2 - iterator for second elements of pairs
      comparator - determines whether two elements are equal and should be paired together
  • Method Details

    • hasNext

      public boolean hasNext(@GuardSatisfied OrderedPairIterator<T extends @Nullable Object> this)
      Specified by:
      hasNext in interface Iterator<T extends @Nullable Object>
    • next

      public MPair<@Nullable T,@Nullable T> next(@GuardSatisfied OrderedPairIterator<T extends @Nullable Object> this)
      Specified by:
      next in interface Iterator<T extends @Nullable Object>
    • remove

      public void remove(@GuardSatisfied OrderedPairIterator<T extends @Nullable Object> this)
      Specified by:
      remove in interface Iterator<T extends @Nullable Object>