public final class MoreCollectors
extends java.lang.Object
java.util.stream.Collectors that are not otherwise associated
with a com.google.common type.| Modifier and Type | Class and Description |
|---|---|
private static class |
MoreCollectors.ToOptionalState
This atrocity is here to let us report several of the elements in the stream if there were more
than one, not just two.
|
| Modifier and Type | Field and Description |
|---|---|
private static java.lang.Object |
NULL_PLACEHOLDER |
private static java.util.stream.Collector<java.lang.Object,?,java.lang.Object> |
ONLY_ELEMENT |
private static java.util.stream.Collector<java.lang.Object,?,java.util.Optional<java.lang.Object>> |
TO_OPTIONAL |
| Modifier | Constructor and Description |
|---|---|
private |
MoreCollectors() |
| Modifier and Type | Method and Description |
|---|---|
static <T> java.util.stream.Collector<T,?,T> |
onlyElement()
A collector that takes a stream containing exactly one element and returns that element.
|
static <T> java.util.stream.Collector<T,?,java.util.Optional<T>> |
toOptional()
A collector that converts a stream of zero or one elements to an
Optional. |
private static final java.util.stream.Collector<java.lang.Object,?,java.util.Optional<java.lang.Object>> TO_OPTIONAL
private static final java.lang.Object NULL_PLACEHOLDER
private static final java.util.stream.Collector<java.lang.Object,?,java.lang.Object> ONLY_ELEMENT
public static <T> java.util.stream.Collector<T,?,java.util.Optional<T>> toOptional()
Optional.Optional.of(onlyElement) if the stream has exactly one element (must not be
null) and returns Optional.empty() if it has none.java.lang.IllegalArgumentException - if the stream consists of two or more elements.java.lang.NullPointerException - if any element in the stream is null.public static <T> java.util.stream.Collector<T,?,T> onlyElement()
IllegalArgumentException if the stream consists of two or
more elements, and a NoSuchElementException if the stream is empty.