Class Pair<A,​B>

  • Type Parameters:
    A - the first element type
    B - the second element type
    All Implemented Interfaces:
    Tuple, Serializable, Comparable<Pair<A,​B>>, org.joda.beans.Bean, org.joda.beans.ImmutableBean

    public final class Pair<A,​B>
    extends Object
    implements org.joda.beans.ImmutableBean, Tuple, Comparable<Pair<A,​B>>, Serializable
    An immutable pair consisting of two elements.

    This implementation refers to the elements as 'first' and 'second'. The elements cannot be null.

    Although the implementation is immutable, there is no restriction on the objects that may be stored. If mutable objects are stored in the pair, then the pair itself effectively becomes mutable.

    Some primitive specializations of this class are provided, such as DoublesPair.

    This class is immutable and thread-safe if the stored objects are immutable.

    See Also:
    Serialized Form
    • Method Detail

      • of

        public static <A,​B> Pair<A,​B> of​(A first,
                                                     B second)
        Obtains a pair inferring the types.
        Type Parameters:
        A - the first element type
        B - the second element type
        Parameters:
        first - the first element
        second - the second element
        Returns:
        a pair formed from the two parameters
      • combining

        public static <A,​B> BinaryOperator<Pair<A,​B>> combining​(BiFunction<? super A,​? super A,​? extends A> combinerFirst,
                                                                            BiFunction<? super B,​? super B,​? extends B> combinerSecond)
        Returns a combiner of pair instances.

        This is useful if you have a stream of Pair<A, B> and would like to reduce.

        e.g

        pairList.stream()
             .reduce(Pair.combining(A::combinedWith, B::combinedWith))
         
        Type Parameters:
        A - the type of the first values
        B - the type of the second values
        Parameters:
        combinerFirst - the combiner of first values
        combinerSecond - the combiner of second values
        Returns:
        the combiner of pair instance
      • combinedWith

        public <C,​D> Pair<A,​B> combinedWith​(Pair<C,​D> other,
                                                        BiFunction<? super A,​? super C,​? extends A> combinerFirst,
                                                        BiFunction<? super B,​? super D,​? extends B> combinerSecond)
        Combines this instance with another.
        Type Parameters:
        C - the type of the first value in the other instance
        D - the type of the second value in the other instance
        Parameters:
        other - the other pair
        combinerFirst - the combiner of first values
        combinerSecond - the combiner of second values
        Returns:
        the combined pair instance
      • size

        public int size()
        Gets the number of elements held by this pair.
        Specified by:
        size in interface Tuple
        Returns:
        size 2
      • elements

        public ImmutableList<Object> elements()
        Gets the elements from this pair as a list.

        The list returns each element in the pair in order.

        Specified by:
        elements in interface Tuple
        Returns:
        the elements as an immutable list
      • compareTo

        public int compareTo​(Pair<A,​B> other)
        Compares the pair based on the first element followed by the second element.

        The element types must be Comparable.

        Specified by:
        compareTo in interface Comparable<A>
        Parameters:
        other - the other pair
        Returns:
        negative if this is less, zero if equal, positive if greater
        Throws:
        ClassCastException - if either object is not comparable
      • toString

        public String toString()
        Gets the pair using a standard string format.

        The standard format is '[$first, $second]'. Spaces around the values are trimmed.

        Overrides:
        toString in class Object
        Returns:
        the pair as a string
      • meta

        public static Pair.Meta meta()
        The meta-bean for Pair.
        Returns:
        the meta-bean, not null
      • metaPair

        public static <R,​S> Pair.Meta<R,​S> metaPair​(Class<R> cls1,
                                                                Class<S> cls2)
        The meta-bean for Pair.
        Type Parameters:
        R - the first generic type
        S - the second generic type
        Parameters:
        cls1 - the first generic type
        cls2 - the second generic type
        Returns:
        the meta-bean, not null
      • metaBean

        public Pair.Meta<A,​B> metaBean()
        Specified by:
        metaBean in interface org.joda.beans.Bean
      • getFirst

        public A getFirst()
        Gets the first element in this pair.
        Returns:
        the value of the property, not null
      • getSecond

        public B getSecond()
        Gets the second element in this pair.
        Returns:
        the value of the property, not null
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object