Package 

Class Pair

  • All Implemented Interfaces:
    java.io.Serializable , java.lang.Comparable , java.util.Map.Entry

    
    public abstract class Pair<L, R>
     implements Map.Entry<L, R>, Comparable<Pair<L, R>>, Serializable
                        

    A pair consisting of two elements.

    This class is an abstract implementation defining the basic API. It refers to the elements as 'left' and 'right'. It also implements the {@code Map.Entry} interface where the key is 'left' and the value is 'right'.

    Subclass implementations may be mutable or immutable. However, there is no restriction on the type of the stored objects that may be stored. If mutable objects are stored in the pair, then the pair itself effectively becomes mutable.

    • Method Summary

      Modifier and Type Method Description
      static <L, R> Pair<L, R> of(L left, R right) Obtains an immutable pair of from two objects inferring the generic types.
      abstract L getLeft() Gets the left element from this pair.
      abstract R getRight() Gets the right element from this pair.
      final L getKey() Gets the key from this pair.
      R getValue() Gets the value from this pair.
      int compareTo(Pair<L, R> other) Compares the pair based on the left element followed by the right element.
      boolean equals(Object obj) Compares this pair to another based on the two elements.
      int hashCode() Returns a suitable hash code.
      String toString() Returns a String representation of this pair using the format {@code ($left,$right)}.
      String toString(String format) Formats the receiver using the given format.
      • Methods inherited from class java.util.Map.Entry

        comparingByKey, comparingByValue, getKey, getValue, setValue
      • Methods inherited from class java.lang.Comparable

        compareTo
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • of

         static <L, R> Pair<L, R> of(L left, R right)

        Obtains an immutable pair of from two objects inferring the generic types.

        This factory allows the pair to be created using inference toobtain the generic types.

        Parameters:
        left - the left element, may be null
        right - the right element, may be null
      • getLeft

         abstract L getLeft()

        Gets the left element from this pair.

        When treated as a key-value pair, this is the key.

      • getRight

         abstract R getRight()

        Gets the right element from this pair.

        When treated as a key-value pair, this is the value.

      • getKey

         final L getKey()

        Gets the key from this pair.

        This method implements the {@code Map.Entry} interface returning theleft element as the key.

      • getValue

         R getValue()

        Gets the value from this pair.

        This method implements the {@code Map.Entry} interface returning theright element as the value.

      • compareTo

         int compareTo(Pair<L, R> other)

        Compares the pair based on the left element followed by the right element.The types must be {@code Comparable}.

        Parameters:
        other - the other pair, not null
      • equals

         boolean equals(Object obj)

        Compares this pair to another based on the two elements.

        Parameters:
        obj - the object to compare to, null returns false
      • hashCode

         int hashCode()

        Returns a suitable hash code.The hash code follows the definition in {@code Map.Entry}.

      • toString

         String toString()

        Returns a String representation of this pair using the format {@code ($left,$right)}.

      • toString

         String toString(String format)

        Formats the receiver using the given format.

        This uses java.util.Formattable to perform the formatting. Two variables maybe used to embed the left and right elements. Use {@code %1$s} for the leftelement (key) and {@code %2$s} for the right element (value).The default format used by {@code toString()} is {@code (%1$s,%2$s)}.

        Parameters:
        format - the format string, optionally containing {@code %1$s} and {@code %2$s}, not null