Class Pair<T1,T2>
- java.lang.Object
-
- net.hydromatic.morel.util.Pair<T1,T2>
-
- Type Parameters:
T1- Left-hand typeT2- Right-hand type
- All Implemented Interfaces:
Serializable,Comparable<Pair<T1,T2>>,Map.Entry<T1,T2>
public class Pair<T1,T2> extends Object implements Comparable<Pair<T1,T2>>, Map.Entry<T1,T2>, Serializable
Pair of objects.Because a pair implements
equals(Object),hashCode()andcompareTo(Pair), it can be used in any kind ofCollection.- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classPair.AdjacentIterator<E>Iterator that returns consecutive pairs of elements from an underlying iterator.private static classPair.FirstAndIterator<E>Iterator that returns the first element of a collection paired with every other element.private static classPair.LeftIterator<L,R>Iterator that returns the left field of each pair.private static classPair.MutableZipList<K,V>A mutable list of pairs backed by a pair of mutable lists.static interfacePair.PairWithOrdinalConsumer<K,V>Represents an operation that accepts two input arguments and an ordinal, and returns no result.private static classPair.RightIterator<L,R>Iterator that returns the right field of each pair.private static classPair.ZipIterator<L,R>Iterator that pairs elements from two iterators.private static classPair.ZipList<K,V>Unmodifiable list of pairs, backed by a pair of lists.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static <T> Iterable<Pair<T,T>>adjacents(Iterable<T> iterable)Returns an iterator that iterates over (i, i + 1) pairs in an iterable.private static <C extends Comparable<C>>
intcompare(C c1, C c2)Compares a pair of comparable values of the same type.intcompareTo(Pair<T1,T2> that)booleanequals(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.static <K,V>
voidforEach(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>
voidforEach(Iterable<K> ks, Iterable<V> vs, BiConsumer<K,V> consumer)Calls a consumer for each pair of items in two iterables.static <K,V>
voidforEachIndexed(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>
voidforEachIndexed(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>
voidforEachIndexed(Map<K,V> map, Pair.PairWithOrdinalConsumer<K,V> consumer)Calls a consumer for each entry in a map.T1getKey()T2getValue()inthashCode()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 <K,V>
Pair<K,V>of(Map.Entry<K,V> entry)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>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)T2setValue(T2 value)static <K,V>
Map<K,V>toMap(Iterable<Pair<K,V>> pairs)Converts a collection of Pairs into a Map.StringtoString()static <K,V>
Iterable<Pair<K,V>>zip(Iterable<? extends K> ks, Iterable<? extends V> vs)Converts two iterables into an iterable ofPairs.static <K,V>
List<Pair<K,V>>zip(List<K> ks, List<V> vs)Converts two lists into a list ofPairs, 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 ofPairs.static <K,V>
List<Pair<K,V>>zip(K[] ks, V[] vs)Converts two arrays into a list ofPairs.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.
-
-
-
Method Detail
-
of
public static <T1,T2> Pair<T1,T2> of(T1 left, T2 right)
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
-
equals
public boolean equals(Object obj)
-
hashCode
public int hashCode()
Computes hash code consistent with
Map.Entry.hashCode().
-
compareTo
public int compareTo(@Nonnull Pair<T1,T2> that)
- Specified by:
compareToin interfaceComparable<T1>
-
compare
private static <C extends Comparable<C>> int compare(C c1, C c2)
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
public static <K,V> Map<K,V> toMap(Iterable<Pair<K,V>> pairs)
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
public static <K,V> List<Pair<K,V>> zip(List<K> ks, List<V> vs)
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:
Ord.zip(java.util.List)
-
zip
public static <K,V> List<Pair<K,V>> zip(List<K> ks, List<V> vs, boolean strict)
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:
Ord.zip(java.util.List)
-
forEach
public 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.
-
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
public static <K,V> void forEachIndexed(Map<K,V> map, Pair.PairWithOrdinalConsumer<K,V> consumer)
Calls a consumer for each entry in a map.
-
zip
public static <K,V> Iterable<Pair<K,V>> zip(Iterable<? extends K> ks, Iterable<? extends V> vs)
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
public static <K,V> List<Pair<K,V>> zip(K[] ks, V[] vs)
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
public 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.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:
Map.forEach(java.util.function.BiConsumer)
-
left
public static <L,R> Iterable<L> left(Iterable<? extends Map.Entry<L,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<L,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
-
adjacents
public static <T> Iterable<Pair<T,T>> adjacents(Iterable<T> iterable)
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
public 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.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
-
-