Class Pair<T1,T2>
- Type Parameters:
T1
- Left-hand typeT2
- Right-hand type
- All Implemented Interfaces:
Serializable
,Comparable<Pair<T1,
,T2>> Map.Entry<T1,
T2>
- Direct Known Subclasses:
RelFieldTrimmer.TrimResult
,RexFieldCollation
Because a pair implements equals(Object)
, hashCode()
and
compareTo(Pair)
, it can be used in any kind of
Collection
.
- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
Represents an operation that accepts two input arguments and an ordinal, and returns no result. -
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionReturns an iterator that iterates over (i, i + 1) pairs in an iterable.int
boolean
Returns an iterator that iterates over (0, i) pairs in an iterable for i > 0.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.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 forEachIndexed
(Iterable<? extends Map.Entry<K, V>> pairs, Pair.PairWithOrdinalConsumer<K, V> consumer) Calls a consumer with an ordinal for each pair of items in an iterable of pairs.static <K,
V> void forEachIndexed
(Iterable<K> ks, Iterable<V> vs, Pair.PairWithOrdinalConsumer<K, V> consumer) Calls a consumer with an ordinal for each pair of items in two iterables.static <K,
V> void forEachIndexed
(Map<K, V> map, Pair.PairWithOrdinalConsumer<K, V> consumer) Calls a consumer for each entry in a map.getKey()
getValue()
int
hashCode()
static <L,
R> Iterable<L> Returns an iterable over the left slice of an iterable.static <K,
V> List<K> static <K,
V> Pair<K, V> Creates aPair
from aMap.Entry
.static <T1,
T2> Pair<T1, T2> of
(T1 left, T2 right) Creates a Pair of appropriate type.static <L,
R> Iterable<R> Returns an iterable over the right slice of an iterable.static <K,
V> List<V> static <K,
V> Map<K, V> Converts a collection of Pairs into a Map.toString()
Converts two iterables into an iterable ofPair
s.Converts two lists into a list ofPair
s, whose length is the lesser of the lengths of the source lists.Converts two lists into a list ofPair
s.zip
(K[] ks, V[] vs) Converts two arrays into a list ofPair
s.zipMutable
(List<K> ks, List<V> vs) Returns a mutable list of pairs backed by a pair of mutable lists.
-
Field Details
-
left
-
right
-
-
Constructor Details
-
Pair
Creates a Pair.- Parameters:
left
- left valueright
- right value
-
-
Method Details
-
of
Creates a Pair of appropriate type.This is a shorthand that allows you to omit implicit types. For example, you can write:
return Pair.of(s, n);
instead ofreturn new Pair<String, Integer>(s, n);
- Parameters:
left
- left valueright
- right value- Returns:
- A Pair
-
of
Creates aPair
from aMap.Entry
. -
equals
-
hashCode
public int hashCode()Computes hash code consistent with
Map.Entry.hashCode()
. -
compareTo
- Specified by:
compareTo
in interfaceComparable<T1>
-
toString
-
getKey
-
getValue
-
setValue
-
toMap
Converts a collection of Pairs into a Map.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.
- Parameters:
pairs
- Collection of Pair objects- Returns:
- map with the same contents as the collection
-
zip
Converts two lists into a list ofPair
s, whose length is the lesser of the lengths of the source lists.- Parameters:
ks
- Left listvs
- Right list- Returns:
- List of pairs
- See Also:
-
zip
public static <K,V> List<Pair<K,V>> zip(List<? extends K> ks, List<? extends V> vs, boolean strict) Converts two lists into a list ofPair
s.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.
- Parameters:
ks
- Left listvs
- Right liststrict
- Whether to fail if lists have different size- Returns:
- List of pairs
- See Also:
-
zip
Converts two iterables into an iterable ofPair
s.The resulting iterator ends whenever the first of the input iterators ends. But typically the source iterators will be the same length.
- Parameters:
ks
- Left iterablevs
- Right iterable- Returns:
- Iterable over pairs
-
zip
Converts two arrays into a list ofPair
s.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.
- Parameters:
ks
- Left arrayvs
- Right array- Returns:
- List of pairs
-
zipMutable
Returns a mutable list of pairs backed by a pair of mutable lists.Modifications to this list are reflected in the backing lists, and vice versa.
- Type Parameters:
K
- Key (left) value typeV
- Value (right) value type
-
forEach
public 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.Calls to the action stop whenever the first of the input iterators ends. But typically the source iterators will be the same length.
- Type Parameters:
K
- Left typeV
- Right type- Parameters:
ks
- Left iterablevs
- Right iterableconsumer
- The action to be performed for each element- See Also:
-
forEachIndexed
public static <K,V> void forEachIndexed(Iterable<K> ks, Iterable<V> vs, Pair.PairWithOrdinalConsumer<K, V> consumer) Calls a consumer with an ordinal for each pair of items in two iterables. -
forEachIndexed
public static <K,V> void forEachIndexed(Iterable<? extends Map.Entry<K, V>> pairs, Pair.PairWithOrdinalConsumer<K, V> consumer) Calls a consumer with an ordinal for each pair of items in an iterable of pairs. -
forEachIndexed
Calls a consumer for each entry in a map. -
forEach
public 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.- Type Parameters:
K
- Left typeV
- Right type- Parameters:
entries
- Pairsconsumer
- The action to be performed for each element- See Also:
-
left
public 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.- Type Parameters:
L
- Left typeR
- Right type- Parameters:
iterable
- Iterable over pairs- Returns:
- Iterable over the left elements
-
right
public 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.- Type Parameters:
L
- right typeR
- Right type- Parameters:
iterable
- Iterable over pairs- Returns:
- Iterable over the right elements
-
left
-
right
-
adjacents
Returns an iterator that iterates over (i, i + 1) pairs in an iterable.For example,
adjacents([3, 5, 7])
returns [(3, 5), (5, 7)].- Type Parameters:
T
- Element type- Parameters:
iterable
- Source collection- Returns:
- Iterable over adjacent element pairs
-
firstAnd
Returns an iterator that iterates over (0, i) pairs in an iterable for i > 0.For example,
firstAnd([3, 5, 7])
returns [(3, 5), (3, 7)].- Type Parameters:
T
- Element type- Parameters:
iterable
- Source collection- Returns:
- Iterable over pairs of the first element and all other elements
-