F
- defines the first type of the tupleS
- defines the second type of the tuplepublic class Tuple<F,S> extends Object
If the first type is comparable and should be used to compare the tuples, ComparableTuple
can be used.
Tuple
,
Comparable
Constructor and Description |
---|
Tuple(F first,
S second)
Creates a tuple with a givens value for first and second
Can be used to specify the generic types for F and S.
|
Modifier and Type | Method and Description |
---|---|
static <F,S> Tuple<F,S> |
create()
Creates a new tuple with both values set to null
|
static <F,S> Tuple<F,S> |
create(F first)
Creates a tuple with a given value for first
|
static <F,S> Tuple<F,S> |
create(F first,
S second)
Creates a tuple with a givens value for first and second
|
boolean |
equals(Object obj) |
static <T extends Tuple<K,V>,K,V> |
firsts(Collection<T> tuples)
Extracts all first components of the given collection of tuples and returns them as list.
|
static <K,V> Stream<Tuple<K,V>> |
flatten(Map.Entry<K,Collection<V>> entry)
Maps an entry which contains a collection as value into a
Stream of tuples, containing
the key of the entry along with a value of the collection. |
static <K,V> List<Tuple<K,V>> |
fromMap(Map<K,V> map)
Converts a map into a list of tuples.
|
F |
getFirst()
Returns the first component of the tuple
|
S |
getSecond()
Returns the second component of the tuple
|
int |
hashCode() |
static <T extends Tuple<K,V>,K,V> |
seconds(Collection<T> tuples)
Extracts all second components of the given collection of tuples and returns them as list.
|
void |
setFirst(F first)
Sets the first component of the tuple to the given value.
|
void |
setSecond(S second)
Sets the second component of the tuple to the given value.
|
static <K,V> Map<K,V> |
toMap(Collection<Tuple<K,V>> values)
Converts a collection of tuples into a map
|
static <K,V> Collector<Tuple<K,V>,Map<K,V>,Map<K,V>> |
toMap(Supplier<Map<K,V>> supplier)
|
static <K,V> Collector<Tuple<K,V>,Map<K,V>,Map<K,V>> |
toMap(Supplier<Map<K,V>> supplier,
BinaryOperator<V> merger)
|
static <K,V> Collector<Tuple<K,V>,MultiMap<K,V>,MultiMap<K,V>> |
toMultiMap(Supplier<MultiMap<K,V>> supplier)
|
String |
toString() |
static <K,V> Tuple<K,V> |
valueOf(Map.Entry<K,V> entry)
Converts a
Map.Entry into a tuple. |
public Tuple(F first, S second)
Can be used to specify the generic types for F and S. Otherwise, the create methods can be used.
first
- defines the value to be used for the first component of the tuplesecond
- defines the value to be used for the second component of the tuplepublic static <F,S> Tuple<F,S> create()
F
- defines the first type of the tupleS
- defines the second type of the tuplepublic static <F,S> Tuple<F,S> create(F first)
F
- defines the first type of the tupleS
- defines the second type of the tuplefirst
- defines the value to be used for the first component of the tuplepublic static <F,S> Tuple<F,S> create(F first, S second)
F
- defines the first type of the tupleS
- defines the second type of the tuplefirst
- defines the value to be used for the first component of the tuplesecond
- defines the value to be used for the second component of the tuplepublic F getFirst()
public void setFirst(F first)
first
- defines the value to be used as the first component of the tuplepublic S getSecond()
public void setSecond(S second)
second
- defines the value to be used as the second component of the tuplepublic static <T extends Tuple<K,V>,K,V> List<K> firsts(@Nonnull Collection<T> tuples)
T
- the type of the tuples involvedK
- the type of the first elements of the tuplesV
- the type of the second elements of the tuplestuples
- the collection of tuples to processpublic static <T extends Tuple<K,V>,K,V> List<V> seconds(@Nonnull Collection<T> tuples)
T
- the type of the tuples involvedK
- the type of the first elements of the tuplesV
- the type of the second elements of the tuplestuples
- the collection of tuples to processpublic static <K,V> List<Tuple<K,V>> fromMap(@Nonnull Map<K,V> map)
K
- the key type of the map and therefore the type of the first component of the tuplesV
- the value type of the map and therefore the type of the second component of the tuplesmap
- the map to be convertedpublic static <K,V> Map<K,V> toMap(@Nonnull Collection<Tuple<K,V>> values)
K
- the key type of the map and therefore the type of the first component of the tuplesV
- the value type of the map and therefore the type of the second component of the tuplesvalues
- the collection of tuples to be convertedpublic static <K,V> Collector<Tuple<K,V>,Map<K,V>,Map<K,V>> toMap(Supplier<Map<K,V>> supplier, BinaryOperator<V> merger)
Collector
which can be used to collect a Stream
of tuples into a Map
.
As an example: aStream.collect(Tuple.toMap(HashMap::new, (a, b) -> b))
will transform the
stream of tuples into a map where a later key value pair will overwrite earlier ones.
K
- key type of the tuples being processedV
- value type of the tuples being processedsupplier
- factory for generating the result mapmerger
- used to decide which value to keep on a key collisiontoMap(java.util.function.Supplier)
public static <K,V> Collector<Tuple<K,V>,Map<K,V>,Map<K,V>> toMap(Supplier<Map<K,V>> supplier)
Collector
which can be used to collect a Stream
of tuples into a Map
.
Key collisions are automatically handled by choosing the later entry (updating the map).
K
- key type of the tuples being processedV
- value type of the tuples being processedsupplier
- factory for generating the result mappublic static <K,V> Collector<Tuple<K,V>,MultiMap<K,V>,MultiMap<K,V>> toMultiMap(Supplier<MultiMap<K,V>> supplier)
Collector
which can be used to collect a Stream
of tuples into a MultiMap
.
The type of MultiMap used can be determined by the supplier. So for example
MultiMap::createOrdered
will create a map with ordered keys.
K
- key type of the tuples being processedV
- value type of the tuples being processedsupplier
- factory for generating the result mappublic static <K,V> Stream<Tuple<K,V>> flatten(Map.Entry<K,Collection<V>> entry)
Stream
of tuples, containing
the key of the entry along with a value of the collection.
This method is designed to be used with Stream.flatMap(java.util.function.Function)
:
map.entrySet().flatMap(e -> Tuple.flatten(e)).forEach(t -> System.out.println(t));
K
- the key type of the entryV
- the value type of the entryentry
- the entry to transformpublic static <K,V> Tuple<K,V> valueOf(Map.Entry<K,V> entry)
Map.Entry
into a tuple.K
- the key type of the entryV
- the value type of the entryentry
- the entry to convertCopyright © 2018. All rights reserved.