Package com.google.common.collect
Class MoreCollectors
- java.lang.Object
-
- com.google.common.collect.MoreCollectors
-
@GwtCompatible public final class MoreCollectors extends Object
Collectors not present injava.util.stream.Collectors
that are not otherwise associated with acom.google.common
type.- Since:
- 21.0
- Author:
- Louis Wasserman
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T extends @Nullable Object>
Collector<T,?,T>onlyElement()
A collector that takes a stream containing exactly one element and returns that element.static <T> Collector<T,?,Optional<T>>
toOptional()
A collector that converts a stream of zero or one elements to anOptional
.
-
-
-
Method Detail
-
toOptional
public static <T> Collector<T,?,Optional<T>> toOptional()
A collector that converts a stream of zero or one elements to anOptional
.- Returns:
Optional.of(onlyElement)
if the stream has exactly one element (must not benull
) and returnsOptional.empty()
if it has none.- Throws:
IllegalArgumentException
- if the stream consists of two or more elements.NullPointerException
- if any element in the stream isnull
.
-
onlyElement
public static <T extends @Nullable Object> Collector<T,?,T> onlyElement()
A collector that takes a stream containing exactly one element and returns that element. The returned collector throws anIllegalArgumentException
if the stream consists of two or more elements, and aNoSuchElementException
if the stream is empty.
-
-