L
- the left element typeR
- the right element typepublic abstract class Pair<L,R>
extends java.lang.Object
implements java.io.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'.
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.
Constructor and Description |
---|
Pair() |
Modifier and Type | Method and Description |
---|---|
boolean |
equals(java.lang.Object obj)
Compares this pair to another based on the two elements.
|
abstract L |
getLeft()
Gets the left element from this pair.
|
abstract R |
getRight()
Gets the right element from this pair.
|
int |
hashCode()
Returns a suitable hash code.
|
static <L,R> Pair<L,R> |
of(L left,
R right)
Obtains an immutable pair of from two objects inferring the generic types.
|
java.lang.String |
toString()
Returns a String representation of this pair using the format
($left,$right) . |
public 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 to obtain the generic types.
L
- the left element typeR
- the right element typeleft
- the left element, may be nullright
- the right element, may be nullpublic abstract L getLeft()
Gets the left element from this pair.
When treated as a key-value pair, this is the key.
public abstract R getRight()
Gets the right element from this pair.
When treated as a key-value pair, this is the value.
public boolean equals(java.lang.Object obj)
Compares this pair to another based on the two elements.
equals
in class java.lang.Object
obj
- the object to compare to, null returns falsepublic int hashCode()
Returns a suitable hash code.
The hash code follows the definition in Map.Entry
.
hashCode
in class java.lang.Object
public java.lang.String toString()
Returns a String representation of this pair using the format ($left,$right)
.
toString
in class java.lang.Object