Package io.vavr

Class Tuple2<T1,T2>

java.lang.Object
io.vavr.Tuple2<T1,T2>
Type Parameters:
T1 - type of the 1st element
T2 - type of the 2nd element
All Implemented Interfaces:
Tuple, Serializable, Comparable<Tuple2<T1,T2>>

public final class Tuple2<T1,T2> extends Object implements Tuple, Comparable<Tuple2<T1,T2>>, Serializable
A tuple of two elements which can be seen as cartesian product of two components.
See Also:
  • Field Details

    • serialVersionUID

      private static final long serialVersionUID
      See Also:
    • _1

      public final T1 _1
      The 1st element of this tuple.
    • _2

      public final T2 _2
      The 2nd element of this tuple.
  • Constructor Details

    • Tuple2

      public Tuple2(T1 t1, T2 t2)
      Constructs a tuple of two elements.
      Parameters:
      t1 - the 1st element
      t2 - the 2nd element
  • Method Details

    • comparator

      public static <T1, T2> Comparator<Tuple2<T1,T2>> comparator(Comparator<? super T1> t1Comp, Comparator<? super T2> t2Comp)
    • compareTo

      private static <U1 extends Comparable<? super U1>, U2 extends Comparable<? super U2>> int compareTo(Tuple2<?,?> o1, Tuple2<?,?> o2)
    • arity

      public int arity()
      Description copied from interface: Tuple
      Returns the number of elements of this tuple.
      Specified by:
      arity in interface Tuple
      Returns:
      the number of elements.
    • compareTo

      public int compareTo(Tuple2<T1,T2> that)
      Specified by:
      compareTo in interface Comparable<T1>
    • _1

      public T1 _1()
      Getter of the 1st element of this tuple.
      Returns:
      the 1st element of this Tuple.
    • update1

      public Tuple2<T1,T2> update1(T1 value)
      Sets the 1st element of this tuple to the given value.
      Parameters:
      value - the new value
      Returns:
      a copy of this tuple with a new value for the 1st element of this Tuple.
    • _2

      public T2 _2()
      Getter of the 2nd element of this tuple.
      Returns:
      the 2nd element of this Tuple.
    • update2

      public Tuple2<T1,T2> update2(T2 value)
      Sets the 2nd element of this tuple to the given value.
      Parameters:
      value - the new value
      Returns:
      a copy of this tuple with a new value for the 2nd element of this Tuple.
    • swap

      public Tuple2<T2,T1> swap()
      Swaps the elements of this Tuple.
      Returns:
      A new Tuple where the first element is the second element of this Tuple and the second element is the first element of this Tuple.
    • toEntry

      public Map.Entry<T1,T2> toEntry()
      Converts the tuple to java.util.Map.Entry Tuple.
      Returns:
      A java.util.Map.Entry where the first element is the key and the second element is the value.
    • map

      public <U1, U2> Tuple2<U1,U2> map(BiFunction<? super T1,? super T2,Tuple2<U1,U2>> mapper)
      Maps the components of this tuple using a mapper function.
      Type Parameters:
      U1 - new type of the 1st component
      U2 - new type of the 2nd component
      Parameters:
      mapper - the mapper function
      Returns:
      A new Tuple of same arity.
      Throws:
      NullPointerException - if mapper is null
    • map

      public <U1, U2> Tuple2<U1,U2> map(Function<? super T1,? extends U1> f1, Function<? super T2,? extends U2> f2)
      Maps the components of this tuple using a mapper function for each component.
      Type Parameters:
      U1 - new type of the 1st component
      U2 - new type of the 2nd component
      Parameters:
      f1 - the mapper function of the 1st component
      f2 - the mapper function of the 2nd component
      Returns:
      A new Tuple of same arity.
      Throws:
      NullPointerException - if one of the arguments is null
    • map1

      public <U> Tuple2<U,T2> map1(Function<? super T1,? extends U> mapper)
      Maps the 1st component of this tuple to a new value.
      Type Parameters:
      U - new type of the 1st component
      Parameters:
      mapper - A mapping function
      Returns:
      a new tuple based on this tuple and substituted 1st component
    • map2

      public <U> Tuple2<T1,U> map2(Function<? super T2,? extends U> mapper)
      Maps the 2nd component of this tuple to a new value.
      Type Parameters:
      U - new type of the 2nd component
      Parameters:
      mapper - A mapping function
      Returns:
      a new tuple based on this tuple and substituted 2nd component
    • apply

      public <U> U apply(BiFunction<? super T1,? super T2,? extends U> f)
      Transforms this tuple to an object of type U.
      Type Parameters:
      U - type of the transformation result
      Parameters:
      f - Transformation which creates a new object of type U based on this tuple's contents.
      Returns:
      An object of type U
      Throws:
      NullPointerException - if f is null
    • toSeq

      public Seq<?> toSeq()
      Description copied from interface: Tuple
      Converts this tuple to a sequence.
      Specified by:
      toSeq in interface Tuple
      Returns:
      A new Seq.
    • append

      public <T3> Tuple3<T1,T2,T3> append(T3 t3)
      Append a value to this tuple.
      Type Parameters:
      T3 - type of the value to append
      Parameters:
      t3 - the value to append
      Returns:
      a new Tuple with the value appended
    • concat

      public <T3> Tuple3<T1,T2,T3> concat(Tuple1<T3> tuple)
      Concat a tuple's values to this tuple.
      Type Parameters:
      T3 - the type of the 3rd value in the tuple
      Parameters:
      tuple - the tuple to concat
      Returns:
      a new Tuple with the tuple values appended
      Throws:
      NullPointerException - if tuple is null
    • concat

      public <T3, T4> Tuple4<T1,T2,T3,T4> concat(Tuple2<T3,T4> tuple)
      Concat a tuple's values to this tuple.
      Type Parameters:
      T3 - the type of the 3rd value in the tuple
      T4 - the type of the 4th value in the tuple
      Parameters:
      tuple - the tuple to concat
      Returns:
      a new Tuple with the tuple values appended
      Throws:
      NullPointerException - if tuple is null
    • concat

      public <T3, T4, T5> Tuple5<T1,T2,T3,T4,T5> concat(Tuple3<T3,T4,T5> tuple)
      Concat a tuple's values to this tuple.
      Type Parameters:
      T3 - the type of the 3rd value in the tuple
      T4 - the type of the 4th value in the tuple
      T5 - the type of the 5th value in the tuple
      Parameters:
      tuple - the tuple to concat
      Returns:
      a new Tuple with the tuple values appended
      Throws:
      NullPointerException - if tuple is null
    • concat

      public <T3, T4, T5, T6> Tuple6<T1,T2,T3,T4,T5,T6> concat(Tuple4<T3,T4,T5,T6> tuple)
      Concat a tuple's values to this tuple.
      Type Parameters:
      T3 - the type of the 3rd value in the tuple
      T4 - the type of the 4th value in the tuple
      T5 - the type of the 5th value in the tuple
      T6 - the type of the 6th value in the tuple
      Parameters:
      tuple - the tuple to concat
      Returns:
      a new Tuple with the tuple values appended
      Throws:
      NullPointerException - if tuple is null
    • concat

      public <T3, T4, T5, T6, T7> Tuple7<T1,T2,T3,T4,T5,T6,T7> concat(Tuple5<T3,T4,T5,T6,T7> tuple)
      Concat a tuple's values to this tuple.
      Type Parameters:
      T3 - the type of the 3rd value in the tuple
      T4 - the type of the 4th value in the tuple
      T5 - the type of the 5th value in the tuple
      T6 - the type of the 6th value in the tuple
      T7 - the type of the 7th value in the tuple
      Parameters:
      tuple - the tuple to concat
      Returns:
      a new Tuple with the tuple values appended
      Throws:
      NullPointerException - if tuple is null
    • concat

      public <T3, T4, T5, T6, T7, T8> Tuple8<T1,T2,T3,T4,T5,T6,T7,T8> concat(Tuple6<T3,T4,T5,T6,T7,T8> tuple)
      Concat a tuple's values to this tuple.
      Type Parameters:
      T3 - the type of the 3rd value in the tuple
      T4 - the type of the 4th value in the tuple
      T5 - the type of the 5th value in the tuple
      T6 - the type of the 6th value in the tuple
      T7 - the type of the 7th value in the tuple
      T8 - the type of the 8th value in the tuple
      Parameters:
      tuple - the tuple to concat
      Returns:
      a new Tuple with the tuple values appended
      Throws:
      NullPointerException - if tuple is null
    • 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