Class Pair<T1,​T2>

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      private static class  Pair.AdjacentIterator<E>
      Iterator that returns consecutive pairs of elements from an underlying iterator.
      private static class  Pair.FirstAndIterator<E>
      Iterator that returns the first element of a collection paired with every other element.
      private static class  Pair.LeftIterator<L,​R>
      Iterator that returns the left field of each pair.
      private static class  Pair.RightIterator<L,​R>
      Iterator that returns the right field of each pair.
      private static class  Pair.ZipIterator<L,​R>
      Iterator that pairs elements from two iterators.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      T1 left  
      T2 right  
    • Constructor Summary

      Constructors 
      Constructor Description
      Pair​(T1 left, T2 right)
      Creates a Pair.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> java.lang.Iterable<Pair<T,​T>> adjacents​(java.lang.Iterable<T> iterable)
      Returns an iterator that iterates over (i, i + 1) pairs in an iterable.
      private static <C extends java.lang.Comparable<C>>
      int
      compare​(C c1, C c2)
      Compares a pair of comparable values of the same type.
      int compareTo​(Pair<T1,​T2> that)  
      boolean equals​(java.lang.Object obj)  
      static <T> java.lang.Iterable<Pair<T,​T>> firstAnd​(java.lang.Iterable<T> iterable)
      Returns an iterator that iterates over (0, i) pairs in an iterable for i > 0.
      T1 getKey()  
      T2 getValue()  
      int hashCode()
      static <L,​R>
      java.lang.Iterable<L>
      left​(java.lang.Iterable<? extends java.util.Map.Entry<L,​R>> iterable)
      Returns an iterable over the left slice of an iterable.
      static <K,​V>
      java.util.List<K>
      left​(java.util.List<? extends java.util.Map.Entry<K,​V>> pairs)  
      static <K,​V>
      Pair<K,​V>
      of​(java.util.Map.Entry<K,​V> entry)
      Creates a Pair from a Map.Entry.
      static <T1,​T2>
      Pair<T1,​T2>
      of​(T1 left, T2 right)
      Creates a Pair of appropriate type.
      static <L,​R>
      java.lang.Iterable<R>
      right​(java.lang.Iterable<? extends java.util.Map.Entry<L,​R>> iterable)
      Returns an iterable over the right slice of an iterable.
      static <K,​V>
      java.util.List<V>
      right​(java.util.List<? extends java.util.Map.Entry<K,​V>> pairs)  
      T2 setValue​(T2 value)  
      static <K,​V>
      java.util.Map<K,​V>
      toMap​(java.lang.Iterable<Pair<K,​V>> pairs)
      Converts a collection of Pairs into a Map.
      java.lang.String toString()  
      static <K,​V>
      java.lang.Iterable<Pair<K,​V>>
      zip​(java.lang.Iterable<? extends K> ks, java.lang.Iterable<? extends V> vs)
      Converts two iterables into an iterable of Pairs.
      static <K,​V>
      java.util.List<Pair<K,​V>>
      zip​(java.util.List<K> ks, java.util.List<V> vs)
      Converts two lists into a list of Pairs, whose length is the lesser of the lengths of the source lists.
      static <K,​V>
      java.util.List<Pair<K,​V>>
      zip​(java.util.List<K> ks, java.util.List<V> vs, boolean strict)
      Converts two lists into a list of Pairs.
      static <K,​V>
      java.util.List<Pair<K,​V>>
      zip​(K[] ks, V[] vs)
      Converts two arrays into a list of Pairs.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • left

        public final T1 left
      • right

        public final T2 right
    • Constructor Detail

      • Pair

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

      • 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​(java.util.Map.Entry<K,​V> entry)
        Creates a Pair from a Map.Entry.
      • equals

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

        public int hashCode()

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

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

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

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

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

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

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

        private static <C extends java.lang.Comparable<C>> int compare​(C c1,
                                                                       C c2)
        Compares a pair of comparable values of the same type. Null collates less than everything else, but equal to itself.
        Parameters:
        c1 - First value
        c2 - Second value
        Returns:
        a negative integer, zero, or a positive integer if c1 is less than, equal to, or greater than c2.
      • toMap

        public static <K,​V> java.util.Map<K,​V> toMap​(java.lang.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> java.util.List<Pair<K,​V>> zip​(java.util.List<K> ks,
                                                                      java.util.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> java.util.List<Pair<K,​V>> zip​(java.util.List<K> ks,
                                                                      java.util.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> java.lang.Iterable<Pair<K,​V>> zip​(java.lang.Iterable<? extends K> ks,
                                                                          java.lang.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> java.util.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
      • left

        public static <L,​R> java.lang.Iterable<L> left​(java.lang.Iterable<? extends java.util.Map.Entry<L,​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> java.lang.Iterable<R> right​(java.lang.Iterable<? extends java.util.Map.Entry<L,​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> java.util.List<K> left​(java.util.List<? extends java.util.Map.Entry<K,​V>> pairs)
      • right

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

        public static <T> java.lang.Iterable<Pair<T,​T>> adjacents​(java.lang.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> java.lang.Iterable<Pair<T,​T>> firstAnd​(java.lang.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