Class Pair<T1,​T2>

java.lang.Object
org.apache.calcite.util.Pair<T1,​T2>
Type Parameters:
T1 - Left-hand type
T2 - Right-hand type
All Implemented Interfaces:
Serializable, Comparable<Pair<T1,​T2>>, Map.Entry<T1,​T2>
Direct Known Subclasses:
RelFieldTrimmer.TrimResult, RexFieldCollation

public class Pair<T1,​T2>
extends Object
implements Comparable<Pair<T1,​T2>>, Map.Entry<T1,​T2>, Serializable
Pair of objects.

Because a pair implements equals(Object), hashCode() and compareTo(Pair), it can be used in any kind of Collection.

See Also:
Serialized Form
  • Field Details

  • Constructor Details

    • Pair

      public Pair​(T1 left, T2 right)
      Creates a Pair.
      Parameters:
      left - left value
      right - right value
  • Method Details

    • of

      public static <T1,​ T2> Pair<T1,​T2> of​(T1 left, T2 right)
      Creates a Pair of appropriate type.

      This is a shorthand that allows you to omit implicit types. For example, you can write:

      return Pair.of(s, n);
      instead of
      return new Pair<String, Integer>(s, n);
      Parameters:
      left - left value
      right - right value
      Returns:
      A Pair
    • of

      public static <K,​ V> Pair<K,​V> of​(Map.Entry<K,​V> entry)
      Creates a Pair from a Map.Entry.
    • equals

      public boolean equals​(Object obj)
      Specified by:
      equals in interface Map.Entry<T1,​T2>
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()

      Computes hash code consistent with Map.Entry.hashCode().

      Specified by:
      hashCode in interface Map.Entry<T1,​T2>
      Overrides:
      hashCode in class Object
    • compareTo

      public int compareTo​(@Nonnull Pair<T1,​T2> that)
      Specified by:
      compareTo in interface Comparable<T1>
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • getKey

      public T1 getKey()
      Specified by:
      getKey in interface Map.Entry<T1,​T2>
    • getValue

      public T2 getValue()
      Specified by:
      getValue in interface Map.Entry<T1,​T2>
    • setValue

      public T2 setValue​(T2 value)
      Specified by:
      setValue in interface Map.Entry<T1,​T2>
    • toMap

      public static <K,​ V> Map<K,​V> toMap​(Iterable<Pair<K,​V>> pairs)
      Converts a collection of Pairs into a Map.

      This is an obvious thing to do because Pair is similar in structure to Map.Entry.

      The map contains a copy of the collection of Pairs; if you change the collection, the map does not change.

      Parameters:
      pairs - Collection of Pair objects
      Returns:
      map with the same contents as the collection
    • zip

      public static <K,​ V> List<Pair<K,​V>> zip​(List<K> ks, List<V> vs)
      Converts two lists into a list of Pairs, whose length is the lesser of the lengths of the source lists.
      Parameters:
      ks - Left list
      vs - Right list
      Returns:
      List of pairs
      See Also:
      Ord.zip(java.util.List)
    • zip

      public static <K,​ V> List<Pair<K,​V>> zip​(List<K> ks, List<V> vs, boolean strict)
      Converts two lists into a list of Pairs.

      The length of the combined list is the lesser of the lengths of the source lists. But typically the source lists will be the same length.

      Parameters:
      ks - Left list
      vs - Right list
      strict - Whether to fail if lists have different size
      Returns:
      List of pairs
      See Also:
      Ord.zip(java.util.List)
    • zip

      public static <K,​ V> Iterable<Pair<K,​V>> zip​(Iterable<? extends K> ks, Iterable<? extends V> vs)
      Converts two iterables into an iterable of Pairs.

      The resulting iterator ends whenever the first of the input iterators ends. But typically the source iterators will be the same length.

      Parameters:
      ks - Left iterable
      vs - Right iterable
      Returns:
      Iterable over pairs
    • zip

      public static <K,​ V> List<Pair<K,​V>> zip​(K[] ks, V[] vs)
      Converts two arrays into a list of Pairs.

      The length of the combined list is the lesser of the lengths of the source arrays. But typically the source arrays will be the same length.

      Parameters:
      ks - Left array
      vs - Right array
      Returns:
      List of pairs
    • zipMutable

      public static <K,​ V> List<Pair<K,​V>> zipMutable​(List<K> ks, List<V> vs)
      Returns a mutable list of pairs backed by a pair of mutable lists.

      Modifications to this list are reflected in the backing lists, and vice versa.

      Type Parameters:
      K - Key (left) value type
      V - Value (right) value type
    • forEach

      public static <K,​ V> void forEach​(Iterable<? extends K> ks, Iterable<? extends V> vs, BiConsumer<K,​V> consumer)
      Applies an action to every element of a pair of iterables.

      Calls to the action stop whenever the first of the input iterators ends. But typically the source iterators will be the same length.

      Type Parameters:
      K - Left type
      V - Right type
      Parameters:
      ks - Left iterable
      vs - Right iterable
      consumer - The action to be performed for each element
      See Also:
      Map.forEach(java.util.function.BiConsumer), Ord.forEach(Iterable, java.util.function.ObjIntConsumer)
    • forEach

      public static <K,​ V> void forEach​(Iterable<? extends Map.Entry<? extends K,​? extends V>> entries, BiConsumer<K,​V> consumer)
      Applies an action to every element of an iterable of pairs.
      Type Parameters:
      K - Left type
      V - Right type
      Parameters:
      entries - Pairs
      consumer - The action to be performed for each element
      See Also:
      Map.forEach(java.util.function.BiConsumer)
    • left

      public static <L,​ R> Iterable<L> left​(Iterable<? extends Map.Entry<? extends L,​? extends R>> iterable)
      Returns an iterable over the left slice of an iterable.
      Type Parameters:
      L - Left type
      R - Right type
      Parameters:
      iterable - Iterable over pairs
      Returns:
      Iterable over the left elements
    • right

      public static <L,​ R> Iterable<R> right​(Iterable<? extends Map.Entry<? extends L,​? extends R>> iterable)
      Returns an iterable over the right slice of an iterable.
      Type Parameters:
      L - right type
      R - Right type
      Parameters:
      iterable - Iterable over pairs
      Returns:
      Iterable over the right elements
    • left

      public static <K,​ V> List<K> left​(List<? extends Map.Entry<? extends K,​? extends V>> pairs)
    • right

      public static <K,​ V> List<V> right​(List<? extends Map.Entry<? extends K,​? extends V>> pairs)
    • adjacents

      public static <T> Iterable<Pair<T,​T>> adjacents​(Iterable<T> iterable)
      Returns an iterator that iterates over (i, i + 1) pairs in an iterable.

      For example, adjacents([3, 5, 7]) returns [(3, 5), (5, 7)].

      Type Parameters:
      T - Element type
      Parameters:
      iterable - Source collection
      Returns:
      Iterable over adjacent element pairs
    • firstAnd

      public static <T> Iterable<Pair<T,​T>> firstAnd​(Iterable<T> iterable)
      Returns an iterator that iterates over (0, i) pairs in an iterable for i > 0.

      For example, firstAnd([3, 5, 7]) returns [(3, 5), (3, 7)].

      Type Parameters:
      T - Element type
      Parameters:
      iterable - Source collection
      Returns:
      Iterable over pairs of the first element and all other elements