Interface Both<A,B>
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
A
and B
.
Usually used as the return type of a function that needs to return two things. For example:
first('=')
.split("k=v") // BiOptional<String, String>
.orElseThrow(...) // Both<String, String>
.andThen(KeyValue::new); // KeyValue
Or:
import static com.google.mu.util.stream.MoreCollectors.partitioningBy;
contacts.stream()
.collect(partitioningBy(Contact::isPrimary, toOptional(), toImmutableList()))
.andThen((primary, secondaries) -> ...);
...
If you have a stream of Both
objects, the following turns it into a BiStream
:
BiStream<String, String> keyValues =
BiStream.from(
first(',')
.repeatedly()
.split("k1=v1,k2=v2,k3=v3")
.map(s -> first('=').split(s).orElseThrow(...)));
Or in a single chained expression:
import static com.google.mu.util.stream.BiStream.toBiStream;
BiStream<String, String> keyValues =
first(',')
.repeatedly()
.split("k1=v1,k2=v2,k3=v3")
.collect(toBiStream(s -> first('=').split(s).orElseThrow(...)));
A stream of Both
can also be collected using a BiCollector
:
import static com.google.mu.util.stream.MoreStreams.mapping;
ImmutableListMultimap<String, String> keyValues =
first(',')
.repeatedly()
.split("k1=v1,k2=v2,k3=v3")
.collect(
mapping(
s -> first('=').split(s).orElseThrow(...),
toImmutableListMultimap()));
Intended as a short-lived intermediary type in a fluent expression (e.g. from MoreCollectors.partitioningBy(java.util.function.Predicate<? super E>, java.util.stream.Collector<E, ?, ? extends R>)
, Substring.Pattern.split(java.lang.CharSequence)
), it's expected that you can either chain fluently using andThen(java.util.function.BiFunction<? super A, ? super B, T>)
, filter(java.util.function.BiPredicate<? super A, ? super B>)
, or directly pass it to common libraries without needing to
extract the two values. If you really have to extract the two values individually though,
consider using toEntry()
and then call the Map.Entry.getKey()
and Map.Entry.getValue()
methods to access them. For example:
import static com.google.mu.util.stream.MoreCollectors.partitioningBy;
var primaryAndSecondaries =
contacts.stream()
.collect(partitioningBy(Contact::isPrimary, toOptional(), toImmutableList()))
.toEntry();
Optional<Contact> primary = primaryAndSecondaries.getKey();
ImmutableList<Contact> secondaries = primaryAndSecondaries.getValue();
...
- Since:
- 5.1
-
Method Summary
Modifier and TypeMethodDescription<T> T
andThen
(BiFunction<? super A, ? super B, T> mapper) Applies themapper
function with this pair of two things as arguments.default BiOptional
<A, B> filter
(BiPredicate<? super A, ? super B> condition) default boolean
matches
(BiPredicate<? super A, ? super B> condition) Returns true if the pair matchescondition
.static <A,
B> Both <A, B> of
(A a, B b) Returns an instance with botha
andb
.peek
(BiConsumer<? super A, ? super B> consumer) Invokesconsumer
with this pair and returns this object as is.default BiOptional
<A, B> skipIf
(BiPredicate<? super A, ? super B> predicate) If the pair matchespredicate
, it's skipped (returns empty).toEntry()
Returns an immutableMap.Entry
holding the pair of values.
-
Method Details
-
of
Returns an instance with botha
andb
.- Since:
- 5.8
-
andThen
Applies themapper
function with this pair of two things as arguments.- Throws:
NullPointerException
- ifmapper
is null
-
filter
- Throws:
NullPointerException
- ifcondition
is null, or ifcondition
matches but either value in this pair is null
-
skipIf
If the pair matchespredicate
, it's skipped (returns empty).- Throws:
NullPointerException
- ifpredicate
is null- Since:
- 6.6
-
matches
Returns true if the pair matchescondition
.- Throws:
NullPointerException
- ifcondition
is null
-
peek
Invokesconsumer
with this pair and returns this object as is.- Throws:
NullPointerException
- ifconsumer
is null
-
toEntry
Returns an immutableMap.Entry
holding the pair of values.- Since:
- 6.5
-