T
- A common type of both elements. Object.class
always works.X
- The type of the first element. Extends <T>.Y
- The type of the second element. Extends <T>.public final class Pair<T,X extends T,Y extends T> extends Object implements Iterable<T>, Cloneable, Serializable
A special characteristic is that each pair has a generic type argument that is a supertype of both elements. As a Cartesian product often uses two related types this can make it easier to work with such a pair.
A pair should never be recursive, that is it should not contain itself directly or indirectly.
Some methods will throw a StackOverflowError
on recursion.
Modifier and Type | Field and Description |
---|---|
X |
first
The first value of this pair.
|
Y |
second
The first value of this pair.
|
Modifier and Type | Method and Description |
---|---|
X |
_1()
Scala-style getter for
first . |
Y |
_2()
Scala-style getter for
second . |
<R> R |
applyTo(BiFunction<X,Y,R> f)
Applies the given function to both elements of this pair.
|
Pair<T,X,Y> |
clone()
Returns this pair, as it is immutable.
|
void |
consumeBy(BiConsumer<X,Y> consumer)
Performs the operation of the given consumer on both elements of this pair.
|
static <TT,TX extends TT,TY extends TT,P extends Pair<TT,TX,TY>,R> |
curry(Function<P,R> f)
Converts a
function on pairs to a function on two elements . |
boolean |
equals(Object obj)
Compares two pairs for equality (by value comparison).
|
int |
hashCode() |
Iterator<T> |
iterator()
Iterator using a type that is shared by both values.
|
static <TT,TX extends TT,TY extends TT> |
of(Class<TT> commonType,
TX first,
TY second)
This creates the Pair and checks the types of both values.
|
static <TT,TX extends TT,TY extends TT> |
of(TX first,
TY second)
Creates a new pair.
|
Spliterator<T> |
spliterator() |
Stream<T> |
stream() |
Pair<T,Y,X> |
swap()
Creates an inverted pair.
|
Object[] |
toArray()
This Pair as an array so that first is on index 0 and second is on index 1.
|
String |
toString()
Returns a string representation of this Pair.
|
static <TX,TY,R> Function<Pair<?,TX,TY>,R> |
uncurry(BiFunction<TX,TY,R> f)
Converts a
function on two elements to a function on pairs . |
public final X extends T first
This is also known as the first coordinate or the left projection of the pair.
public static <TT,TX extends TT,TY extends TT,P extends Pair<TT,TX,TY>,R> BiFunction<TX,TY,R> curry(Function<P,R> f)
function on pairs
to a function on two elements
.
Note: The compiler should be able to infer all types. If not then a lambda should be used
instead (see example below). The returned value is a BiFunction
, not a function that
allows partial application. For that it would have to return
Function<A, Function<B, C>>
instead of BiFunction<A, B, C>
.
Example:
Given the following set:
Set<Pair<A, B, C>> set = new HashSet<>();
We can curry the add method:
Pair.curry(set::add)
This is equivalent to this lambda:
(a, b) -> set.add(Pair.of(a, b))
TT
- Common type. This isn't actually used.TX
- Type of first element. Extends TT.TY
- Type of second element. Extends TT.P
- Actual type of the Pair. This is exactly Pair<TT, TX, TY>
.R
- Return type of the given function f.f
- A function that takes a Pair.uncurry(BiFunction)
public static <TT,TX extends TT,TY extends TT> Pair<TT,TX,TY> of(Class<TT> commonType, TX first, TY second)
The common type is checked at construction, but not available later.
TT
- Common typeTX
- Type of first element. Extends TT.TY
- Type of second element. Extends TT.commonType
- The type that both elements implement.first
- The first element.second
- The second element.ClassCastException
- If and of the two elements is not assignable to commonType.NullPointerException
- If any of the elements is null.public static <TT,TX extends TT,TY extends TT> Pair<TT,TX,TY> of(TX first, TY second)
TT
- Common typeTX
- Type of first element. Extends TT.TY
- Type of second element. Extends TT.first
- The first element.second
- The second element.NullPointerException
- If any of the elements is null.public static <TX,TY,R> Function<Pair<?,TX,TY>,R> uncurry(BiFunction<TX,TY,R> f)
function on two elements
to a function on pairs
.TX
- Type of first element.TY
- Type of second element.R
- Return type of f.f
- A BiFunction that takes two elements.curry(Function)
,
applyTo(BiFunction)
public X _1()
first
.first
public Y _2()
second
.second
public <R> R applyTo(BiFunction<X,Y,R> f)
R
- return type.f
- A function on two elements.NullPointerException
- if f is nulluncurry(BiFunction)
public void consumeBy(BiConsumer<X,Y> consumer)
consumer
- A consumer of two elements.NullPointerException
- if consumer is nulluncurry(BiFunction)
public boolean equals(Object obj)
public int hashCode()
public Iterator<T> iterator()
public Spliterator<T> spliterator()
spliterator
in interface Iterable<T>
public Pair<T,Y,X> swap()
(a, b) → (b, a)
new Pair<>(this.second, this.first)
public Object[] toArray()
new Object[] { this.first, this.second };
public String toString()
This could lead to recursion in rare cases. Pairs, Collections, Arrays, References etc. are
always represented by their type. But this can still lead to a StackOverflowError
.