Class Pair<T1,T2>
- java.lang.Object
-
- org.apache.calcite.util.Pair<T1,T2>
-
- Type Parameters:
T1
- Left-hand typeT2
- Right-hand type
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Comparable<Pair<T1,T2>>
,java.util.Map.Entry<T1,T2>
- Direct Known Subclasses:
Interpreter.Edge
,ReflectiveSqlOperatorTable.Key
,RelBuilder.Field
,RelFieldTrimmer.TrimResult
,RexFieldCollation
public class Pair<T1,T2> extends java.lang.Object implements java.lang.Comparable<Pair<T1,T2>>, java.util.Map.Entry<T1,T2>, java.io.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 class
Pair.AdjacentIterator<E>
Iterator that returns consecutive pairs of elements from an underlying iterator.private static class
Pair.FirstAndIterator<E>
Iterator that returns the first element of a collection paired with every other element.private static class
Pair.LeftIterator<L,R>
Iterator that returns the left field of each pair.private static class
Pair.RightIterator<L,R>
Iterator that returns the right field of each pair.private static class
Pair.ZipIterator<L,R>
Iterator that pairs elements from two iterators.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static <T> java.lang.Iterable<Pair<T,T>>
adjacents(java.lang.Iterable<T> iterable)
Returns an iterator that iterates over (i, i + 1) pairs in an iterable.private static <C extends java.lang.Comparable<C>>
intcompare(C c1, C c2)
Compares a pair of comparable values of the same type.int
compareTo(Pair<T1,T2> that)
boolean
equals(java.lang.Object obj)
static <T> java.lang.Iterable<Pair<T,T>>
firstAnd(java.lang.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>
java.lang.Iterable<L>left(java.lang.Iterable<? extends java.util.Map.Entry<L,R>> iterable)
Returns an iterable over the left slice of an iterable.static <K,V>
java.util.List<K>left(java.util.List<? extends java.util.Map.Entry<K,V>> pairs)
static <K,V>
Pair<K,V>of(java.util.Map.Entry<K,V> entry)
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>
java.lang.Iterable<R>right(java.lang.Iterable<? extends java.util.Map.Entry<L,R>> iterable)
Returns an iterable over the right slice of an iterable.static <K,V>
java.util.List<V>right(java.util.List<? extends java.util.Map.Entry<K,V>> pairs)
T2
setValue(T2 value)
static <K,V>
java.util.Map<K,V>toMap(java.lang.Iterable<Pair<K,V>> pairs)
Converts a collection of Pairs into a Map.java.lang.String
toString()
static <K,V>
java.lang.Iterable<Pair<K,V>>zip(java.lang.Iterable<? extends K> ks, java.lang.Iterable<? extends V> vs)
Converts two iterables into an iterable ofPair
s.static <K,V>
java.util.List<Pair<K,V>>zip(java.util.List<K> ks, java.util.List<V> vs)
Converts two lists into a list ofPair
s, whose length is the lesser of the lengths of the source lists.static <K,V>
java.util.List<Pair<K,V>>zip(java.util.List<K> ks, java.util.List<V> vs, boolean strict)
Converts two lists into a list ofPair
s.static <K,V>
java.util.List<Pair<K,V>>zip(K[] ks, V[] vs)
Converts two arrays into a list ofPair
s.
-
-
-
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
-
of
public static <K,V> Pair<K,V> of(java.util.Map.Entry<K,V> entry)
Creates aPair
from aMap.Entry
.
-
equals
public boolean equals(java.lang.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:
compareTo
in interfacejava.lang.Comparable<T1>
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
compare
private static <C extends java.lang.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> java.util.Map<K,V> toMap(java.lang.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> java.util.List<Pair<K,V>> zip(java.util.List<K> ks, java.util.List<V> vs)
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:
Ord.zip(java.util.List)
-
zip
public static <K,V> java.util.List<Pair<K,V>> zip(java.util.List<K> ks, java.util.List<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:
Ord.zip(java.util.List)
-
zip
public static <K,V> java.lang.Iterable<Pair<K,V>> zip(java.lang.Iterable<? extends K> ks, java.lang.Iterable<? extends V> vs)
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
public static <K,V> java.util.List<Pair<K,V>> zip(K[] ks, V[] vs)
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
-
left
public static <L,R> java.lang.Iterable<L> left(java.lang.Iterable<? extends java.util.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> java.lang.Iterable<R> right(java.lang.Iterable<? extends java.util.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
-
left
public static <K,V> java.util.List<K> left(java.util.List<? extends java.util.Map.Entry<K,V>> pairs)
-
right
public static <K,V> java.util.List<V> right(java.util.List<? extends java.util.Map.Entry<K,V>> pairs)
-
adjacents
public static <T> java.lang.Iterable<Pair<T,T>> adjacents(java.lang.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> java.lang.Iterable<Pair<T,T>> firstAnd(java.lang.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
-
-