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, java.util.function.BiFunction<? super java.lang.String, ? super java.lang.String, ? extends T>)), 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> TandThen(BiFunction<? super A, ? super B, T> mapper) Applies themapperfunction with this pair of two things as arguments.default BiOptional<A, B> filter(BiPredicate<? super A, ? super B> condition) default booleanmatches(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 bothaandb.peek(BiConsumer<? super A, ? super B> consumer) Invokesconsumerwith 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.Entryholding the pair of values.
-
Method Details
-
of
-
andThen
Applies themapperfunction with this pair of two things as arguments.- Throws:
NullPointerException- ifmapperis null
-
filter
- Throws:
NullPointerException- ifconditionis null, or ifconditionmatches but either value in this pair is null
-
skipIf
If the pair matchespredicate, it's skipped (returns empty).- Throws:
NullPointerException- ifpredicateis null- Since:
- 6.6
-
matches
Returns true if the pair matchescondition.- Throws:
NullPointerException- ifconditionis null
-
peek
Invokesconsumerwith this pair and returns this object as is.- Throws:
NullPointerException- ifconsumeris null
-
toEntry
-