L - the left element typeR - the right element typepublic abstract class Pair<L,R> extends Object implements Map.Entry<L,R>, Comparable<Pair<L,R>>, Serializable
这个类是一个定义基本API的抽象实现
它表示元素为“left”和“right” 它还实现了
Map.Entry接口,其中键为'left',值为'right'.
子类实现可以是可变的,也可以是不可变的
但是,对可能存储的存储对象的类型没有限制
如果可变对象存储在对中,那么对本身就会有效地变成可变的
| Constructor and Description |
|---|
Pair() |
| Modifier and Type | Method and Description |
|---|---|
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.
|
L |
getKey()
Gets the key from this pair.
|
abstract L |
getLeft()
Gets the left element from this pair.
|
abstract R |
getRight()
Gets the right element from this pair.
|
R |
getValue()
Gets the value 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 two objects inferring the generic types.
|
String |
toString()
Returns a String representation of this pair using the format
($left,$right). |
String |
toString(String format)
Formats the receiver using the given format.
|
clone, finalize, getClass, notify, notifyAll, wait, wait, waitcomparingByKey, comparingByKey, comparingByValue, comparingByValue, setValuepublic static <L,R> Pair<L,R> of(L left, R right)
Obtains an immutable pair of 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 final L getKey()
Gets the key from this pair.
This method implements the Map.Entry interface returning the
left element as the key.
public R getValue()
Gets the value from this pair.
This method implements the Map.Entry interface returning the
right element as the value.
public int compareTo(Pair<L,R> other)
Compares the pair based on the left element followed by the right element.
The types must be Comparable.
compareTo in interface Comparable<Pair<L,R>>other - the other pair, not nullpublic boolean equals(Object obj)
Compares this pair to another based on the two elements.
public int hashCode()
Returns a suitable hash code.
The hash code follows the definition in Map.Entry.
public String toString()
Returns a String representation of this pair using the format ($left,$right).
public String toString(String format)
Formats the receiver using the given format.
This uses Formattable to perform the formatting. Two variables may
be used to embed the left and right elements. Use %1$s for the left
element (key) and %2$s for the right element (value).
The default format used by toString() is (%1$s,%2$s).
format - the format string, optionally containing %1$s and %2$s, not nullCopyright © 2019. All rights reserved.