java.lang.Object
dk.cloudcreate.essentials.shared.functional.tuple.Pair<T1,T2>
Type Parameters:
T1 - the first element type
T2 - the second element type
All Implemented Interfaces:
Tuple<Pair<T1,T2>>, Serializable

public class Pair<T1,T2> extends Object implements Tuple<Pair<T1,T2>>
Represents a Tuple with two elements.
Note: Pair supports equals(Object) comparison using subclasses, e.g.:

     public class LeftAndRightSide extends Pair<String, String> {

         public LeftAndRightSide(String leftSide, String rightSide) {
             super(leftSide, rightSide);
         }
     }
 
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    final T1
    The first element in this tuple
    final T2
    The second element in this tuple
  • Constructor Summary

    Constructors
    Constructor
    Description
    Pair(T1 t1, T2 t2)
    Construct a new Tuple with 2 elements
  • Method Summary

    Modifier and Type
    Method
    Description
    _1()
    The first element in this tuple
    _2()
    The second element in this tuple
    int
    Number of arguments/elements in the Tuple
    boolean
     
    int
     
    <R1, R2> Pair<R1,R2>
    map(BiFunction<? super T1,? super T2,Pair<R1,R2>> mappingFunction)
    Maps the elements of this Pair using the mapping function
    <R1, R2> Pair<R1,R2>
    map(Function<? super T1,? super R1> mappingFunction1, Function<? super T2,? super R2> mappingFunction2)
    Maps the elements of this Pair using two distinct mapping functions
    <R1> Pair<R1,T2>
    map1(Function<? super T1,? super R1> mappingFunction1)
    Map the first element of this Pair using the mapping function
    <R2> Pair<T1,R2>
    map2(Function<? super T2,? super R2> mappingFunction2)
    Map the second element of this Pair using the mapping function
    static <T1, T2> Pair<T1,T2>
    of(T1 t1, T2 t2)
    Create a new Tuple with 2 elements
    Swap the elements of this Pair
    Converts the Pair to a Map Map.Entry
    List<?>
    Convert the Tuple to a list
     

    Methods inherited from class java.lang.Object

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

    • _1

      public final T1 _1
      The first element in this tuple
    • _2

      public final T2 _2
      The second element in this tuple
  • Constructor Details

    • Pair

      public Pair(T1 t1, T2 t2)
      Construct a new Tuple with 2 elements
      Parameters:
      t1 - the first element
      t2 - the second element
  • Method Details

    • of

      public static <T1, T2> Pair<T1,T2> of(T1 t1, T2 t2)
      Create a new Tuple with 2 elements
      Type Parameters:
      T1 - the first element type
      T2 - the second element type
      Parameters:
      t1 - the first element
      t2 - the second element
      Returns:
      the newly created Pair tuple
    • arity

      public int arity()
      Description copied from interface: Tuple
      Number of arguments/elements in the Tuple
      Specified by:
      arity in interface Tuple<T1>
      Returns:
      Number of arguments/elements in the Tuple
    • toList

      public List<?> toList()
      Description copied from interface: Tuple
      Convert the Tuple to a list
      Specified by:
      toList in interface Tuple<T1>
      Returns:
      list of all Tuple elements
    • _1

      public T1 _1()
      The first element in this tuple
      Returns:
      The first element in this tuple
    • _2

      public T2 _2()
      The second element in this tuple
      Returns:
      The second element in this tuple
    • swap

      public Pair<T2,T1> swap()
      Swap the elements of this Pair
      Returns:
      A new Pair where the first element is the second element of this Pair AND where the second element is the first element of this Pair
    • toEntry

      public Map.Entry<T1,T2> toEntry()
      Converts the Pair to a Map Map.Entry
      Returns:
      a Map.Entry where the key is the first element of this Pair and the value is the second element of this Pair
    • map

      public <R1, R2> Pair<R1,R2> map(BiFunction<? super T1,? super T2,Pair<R1,R2>> mappingFunction)
      Maps the elements of this Pair using the mapping function
      Type Parameters:
      R1 - result type for first element of the Pair after applying the mapping function
      R2 - result type for second element of the Pair after applying the mapping function
      Parameters:
      mappingFunction - the mapping function
      Returns:
      a new Pair with the result of applying the mapping function to this Pair
    • map

      public <R1, R2> Pair<R1,R2> map(Function<? super T1,? super R1> mappingFunction1, Function<? super T2,? super R2> mappingFunction2)
      Maps the elements of this Pair using two distinct mapping functions
      Type Parameters:
      R1 - result type for first element of the Pair after applying the mapping function
      R2 - result type for second element of the Pair after applying the mapping function
      Parameters:
      mappingFunction1 - the mapping function for element number 1
      mappingFunction2 - the mapping function for element number 2
      Returns:
      a new Pair with the result of applying the mapping function to this Pair
    • map1

      public <R1> Pair<R1,T2> map1(Function<? super T1,? super R1> mappingFunction1)
      Map the first element of this Pair using the mapping function
      Type Parameters:
      R1 - result type for first element of the Pair after applying the mapping function
      Parameters:
      mappingFunction1 - the mapping function for element number 1
      Returns:
      a new Pair with the result of applying the mapping function to the first element of this Pair
    • map2

      public <R2> Pair<T1,R2> map2(Function<? super T2,? super R2> mappingFunction2)
      Map the second element of this Pair using the mapping function
      Type Parameters:
      R2 - result type for second element of the Pair after applying the mapping function
      Parameters:
      mappingFunction2 - the mapping function for element number 2
      Returns:
      a new Pair with the result of applying the mapping function to the second element of this Pair
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object