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<? extends T> iterable)
Returns an iterator that iterates over (i, i + 1) pairs in an iterable.
|
int |
compareTo(Pair<T1,T2> that) |
boolean |
equals(@Nullable Object obj) |
static <T> Iterable<Pair<T,T>> |
firstAnd(Iterable<? extends T> iterable)
Returns an iterator that iterates over (0, i) pairs in an iterable for
i > 0.
|
static <K,V> void |
forEach(Iterable<? extends K> ks,
Iterable<? extends V> vs,
BiConsumer<? super K,? super V> consumer)
Applies an action to every element of a pair of iterables.
|
static <K,V> void |
forEach(Iterable<? extends Map.Entry<? extends K,? extends V>> entries,
BiConsumer<? super K,? super V> consumer)
Applies an action to every element of an iterable of pairs.
|
T1 |
getKey() |
T2 |
getValue() |
int |
hashCode() |
static <L,R> Iterable<L> |
left(Iterable<? extends Map.Entry<? extends L,? extends R>> iterable)
Returns an iterable over the left slice of an iterable.
|
static <K,V> List<K> |
left(List<? extends Map.Entry<? extends K,? extends V>> pairs) |
static <K,V> Pair<K,V> |
of(Map.Entry<? extends K,? extends V> entry)
Creates a
Pair from a Map.Entry. |
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<? extends L,? extends R>> iterable)
Returns an iterable over the right slice of an iterable.
|
static <K,V> List<V> |
right(List<? extends Map.Entry<? extends K,? extends V>> pairs) |
T2 |
setValue(T2 value) |
static <K,V> Map<K,V> |
toMap(Iterable<? extends Pair<? extends K,? extends V>> pairs)
Converts a collection of Pairs into a Map.
|
String |
toString() |
static <K,V> Iterable<Pair<K,V>> |
zip(Iterable<? extends K> ks,
Iterable<? extends 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<? extends K> ks,
List<? extends 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<? extends K> ks,
List<? extends V> vs,
boolean strict)
Converts two lists into a list of
Pairs. |
static <K,V> List<Pair<K,V>> |
zipMutable(List<K> ks,
List<V> vs)
Returns a mutable list of pairs backed by a pair of mutable lists.
|
clone, finalize, getClass, notify, notifyAll, wait, wait, waitcomparingByKey, comparingByKey, comparingByValue, comparingByValuepublic 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 static <K,V> Pair<K,V> of(Map.Entry<? extends K,? extends V> entry)
Pair from a Map.Entry.public boolean equals(@Nullable Object obj)
public int hashCode()
Computes hash code consistent with
Map.Entry.hashCode().
public int compareTo(Pair<T1,T2> that)
compareTo in interface Comparable<Pair<T1,T2>>public static <K,V> Map<K,V> toMap(Iterable<? extends Pair<? extends K,? extends 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<? extends K> ks, List<? extends 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<? extends K> ks, List<? extends 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<? extends K> ks, Iterable<? extends 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 <K,V> List<Pair<K,V>> zipMutable(List<K> ks, List<V> vs)
Modifications to this list are reflected in the backing lists, and vice versa.
K - Key (left) value typeV - Value (right) value typepublic static <K,V> void forEach(Iterable<? extends K> ks, Iterable<? extends V> vs, BiConsumer<? super K,? super V> consumer)
Calls to the action stop whenever the first of the input iterators ends. But typically the source iterators will be the same length.
K - Left typeV - Right typeks - Left iterablevs - Right iterableconsumer - The action to be performed for each elementMap.forEach(java.util.function.BiConsumer),
Ord.forEach(Iterable, java.util.function.ObjIntConsumer)public static <K,V> void forEach(Iterable<? extends Map.Entry<? extends K,? extends V>> entries, BiConsumer<? super K,? super V> consumer)
K - Left typeV - Right typeentries - Pairsconsumer - The action to be performed for each elementMap.forEach(java.util.function.BiConsumer)public static <L,R> Iterable<L> left(Iterable<? extends Map.Entry<? extends L,? extends R>> iterable)
L - Left typeR - Right typeiterable - Iterable over pairspublic static <L,R> Iterable<R> right(Iterable<? extends Map.Entry<? extends L,? extends R>> iterable)
L - right typeR - Right typeiterable - Iterable over pairspublic static <T> Iterable<Pair<T,T>> adjacents(Iterable<? extends 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<? extends T> iterable)
For example, firstAnd([3, 5, 7]) returns [(3, 5), (3, 7)].
T - Element typeiterable - Source collectionCopyright © 2012-2022 Apache Software Foundation. All Rights Reserved.