Class Pair<T1,​T2>

    • 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
      • compare

        private static <C extends 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> 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)
      • forEach

        public static <K,​V> void forEach​(Iterable<K> ks,
                                               Iterable<V> vs,
                                               BiConsumer<K,​V> consumer)
        Calls a consumer for each pair of items in two iterables.
      • forEachIndexed

        public static <K,​V> void forEachIndexed​(Iterable<K> ks,
                                                      Iterable<V> vs,
                                                      Pair.PairWithOrdinalConsumer<K,​V> consumer)
        Calls a consumer with an ordinal for each pair of items in two iterables.
      • forEachIndexed

        public static <K,​V> void forEachIndexed​(Iterable<? extends Map.Entry<K,​V>> pairs,
                                                      Pair.PairWithOrdinalConsumer<K,​V> consumer)
        Calls a consumer with an ordinal for each pair of items in an iterable of pairs.
      • forEachIndexed

        public static <K,​V> void forEachIndexed​(Map<K,​V> map,
                                                      Pair.PairWithOrdinalConsumer<K,​V> consumer)
        Calls a consumer for each entry in a map.
      • 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 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<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> Iterable<R> right​(Iterable<? extends 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> List<K> left​(List<? extends Map.Entry<K,​V>> pairs)
      • right

        public static <K,​V> List<V> right​(List<? extends Map.Entry<K,​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