T1 - Left-hand typeT2 - Right-hand typepublic class Pair<T1,T2> extends Object implements Comparable<Pair<T1,T2>>, Map.Entry<T1,T2>, Serializable
Because a pair implements equals(Object), hashCode() and
compareTo(Pair), it can be used in any kind of
Collection.
| Constructor and Description |
|---|
Pair(T1 left,
T2 right)
Creates a Pair.
|
| Modifier and Type | Method and Description |
|---|---|
static <T> Iterable<Pair<T,T>> |
adjacents(Iterable<T> iterable)
Returns an iterator that iterates over (i, i + 1) pairs in an iterable.
|
int |
compareTo(Pair<T1,T2> that) |
boolean |
equals(Object obj) |
static <T> Iterable<Pair<T,T>> |
firstAnd(Iterable<T> iterable)
Returns an iterator that iterates over (0, i) pairs in an iterable for
i > 0.
|
T1 |
getKey() |
T2 |
getValue() |
int |
hashCode() |
static <L,R> Iterable<L> |
left(Iterable<? extends Map.Entry<L,R>> iterable)
Returns an iterable over the left slice of an iterable.
|
static <K,V> List<K> |
left(List<? extends Map.Entry<K,V>> pairs) |
static <T1,T2> Pair<T1,T2> |
of(T1 left,
T2 right)
Creates a Pair of appropriate type.
|
static <L,R> Iterable<R> |
right(Iterable<? extends Map.Entry<L,R>> iterable)
Returns an iterable over the right slice of an iterable.
|
static <K,V> List<V> |
right(List<? extends Map.Entry<K,V>> pairs) |
T2 |
setValue(T2 value) |
static <K,V> Map<K,V> |
toMap(Collection<Pair<K,V>> pairs)
Converts a collection of Pairs into a Map.
|
String |
toString() |
static <K,V> Iterable<Pair<K,V>> |
zip(Iterable<K> ks,
Iterable<V> vs)
Converts two iterables into an iterable of
Pairs. |
static <K,V> List<Pair<K,V>> |
zip(K[] ks,
V[] vs)
Converts two arrays into a list of
Pairs. |
static <K,V> List<Pair<K,V>> |
zip(List<K> ks,
List<V> vs)
Converts two lists into a list of
Pairs,
whose length is the lesser of the lengths of the
source lists. |
static <K,V> List<Pair<K,V>> |
zip(List<K> ks,
List<V> vs,
boolean strict)
Converts two lists into a list of
Pairs. |
public static <T1,T2> Pair<T1,T2> of(T1 left, T2 right)
This is a shorthand that allows you to omit implicit types. For example, you can write:
return Pair.of(s, n);instead of
return new Pair<String, Integer>(s, n);
left - left valueright - right valuepublic boolean equals(Object obj)
public int hashCode()
public int compareTo(Pair<T1,T2> that)
compareTo in interface Comparable<Pair<T1,T2>>public static <K,V> Map<K,V> toMap(Collection<Pair<K,V>> pairs)
This is an obvious thing to do because Pair is similar in structure to
Map.Entry.
The map contains a copy of the collection of Pairs; if you change the collection, the map does not change.
pairs - Collection of Pair objectspublic static <K,V> List<Pair<K,V>> zip(List<K> ks, List<V> vs)
Pairs,
whose length is the lesser of the lengths of the
source lists.ks - Left listvs - Right listOrd.zip(java.util.List)public static <K,V> List<Pair<K,V>> zip(List<K> ks, List<V> vs, boolean strict)
Pairs.
The length of the combined list is the lesser of the lengths of the source lists. But typically the source lists will be the same length.
ks - Left listvs - Right liststrict - Whether to fail if lists have different sizeOrd.zip(java.util.List)public static <K,V> Iterable<Pair<K,V>> zip(Iterable<K> ks, Iterable<V> vs)
Pairs.
The resulting iterator ends whenever the first of the input iterators ends. But typically the source iterators will be the same length.
ks - Left iterablevs - Right iterablepublic static <K,V> List<Pair<K,V>> zip(K[] ks, V[] vs)
Pairs.
The length of the combined list is the lesser of the lengths of the source arrays. But typically the source arrays will be the same length.
ks - Left arrayvs - Right arraypublic static <L,R> Iterable<L> left(Iterable<? extends Map.Entry<L,R>> iterable)
L - Left typeR - Right typeiterable - Iterable over pairspublic static <L,R> Iterable<R> right(Iterable<? extends Map.Entry<L,R>> iterable)
L - right typeR - Right typeiterable - Iterable over pairspublic static <T> Iterable<Pair<T,T>> adjacents(Iterable<T> iterable)
For example, adjacents([3, 5, 7]) returns [(3, 5), (5, 7)].
T - Element typeiterable - Source collectionpublic static <T> Iterable<Pair<T,T>> firstAnd(Iterable<T> iterable)
For example, firstAnd([3, 5, 7]) returns [(3, 5), (3, 7)].
T - Element typeiterable - Source collectionCopyright © 2012–2015 The Apache Software Foundation. All rights reserved.