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>
Because a pair implements equals(Object), hashCode() and
compareTo(Pair), it can be used in any kind of
Collection.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static classIterator that returns consecutive pairs of elements from an underlying iterator.private static classIterator that returns the first element of a collection paired with every other element.private static classIterator that returns the left field of each pair.private static classA mutable list of pairs backed by a pair of mutable lists.static interfaceRepresents an operation that accepts two input arguments and an ordinal, and returns no result.private static classIterator that returns the right field of each pair.private static classIterator that pairs elements from two iterators.private static classUnmodifiable list of pairs, backed by a pair of lists. -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionReturns an iterator that iterates over (i, i + 1) pairs in an iterable.static <K,V> boolean allMatch(Iterable<K> ks, Iterable<V> vs, BiPredicate<K, V> predicate) Returns whether all pairs match a given predicate.static <K,V> boolean anyMatch(Iterable<K> ks, Iterable<V> vs, BiPredicate<K, V> predicate) Returns whether any pair matches a given predicate.private static <C extends Comparable<C>>
intcompare(C c1, C c2) Compares a pair of comparable values of the same type.intbooleanReturns 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<K, V> consumer) Applies an action to every element of an iterable of pairs.static <K,V> void forEach(Iterable<K> ks, Iterable<V> vs, BiConsumer<K, V> consumer) Calls a consumer for each pair of items in two 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()inthashCode()static <L,R> Iterable <L> Returns an iterable over the left slice of an iterable.static <K,V> List <K> static <K,V> boolean noneMatch(Iterable<K> ks, Iterable<V> vs, BiPredicate<K, V> predicate) Returns whether no pair matches a given predicate.static <K,V> Pair <K, V> Creates aPairfrom 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 ofPairs.Converts two lists into a list ofPairs, whose length is the lesser of the lengths of the source lists.Converts two lists into a list ofPairs.zip(K[] ks, V[] vs) Converts two arrays into a list ofPairs.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 aPairfrom aMap.Entry. -
equals
-
hashCode
public int hashCode()Computes hash code consistent with
Map.Entry.hashCode(). -
compareTo
- Specified by:
compareToin interfaceComparable<T1>
-
toString
-
getKey
-
getValue
-
setValue
-
compare
Compares a pair of comparable values of the same type. Null collates less than everything else, but equal to itself.- Parameters:
c1- First valuec2- Second value- Returns:
- a negative integer, zero, or a positive integer if c1 is less than, equal to, or greater than c2.
-
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 ofPairs, 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
Converts two lists into a list ofPairs.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:
-
allMatch
Returns whether all pairs match a given predicate. -
noneMatch
Returns whether no pair matches a given predicate. -
anyMatch
Returns whether any pair matches a given predicate. -
forEach
Calls a consumer for each pair of items in two iterables. -
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. -
zip
Converts two iterables into an iterable ofPairs.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 ofPairs.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 Map.Entry<? extends K, ? extends V>> entries, BiConsumer<K, 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
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
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
-