Package com.google.common.collect
Class MoreCollectors
- java.lang.Object
- 
- com.google.common.collect.MoreCollectors
 
- 
 @GwtCompatible public final class MoreCollectors extends java.lang.Object Collectors not present injava.util.stream.Collectorsthat are not otherwise associated with acom.google.commontype.- Since:
- 21.0
- Author:
- Louis Wasserman
 
- 
- 
Method SummaryAll Methods Static Methods Concrete Methods Modifier and Type Method Description static <T extends @Nullable java.lang.Object>
 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 anOptional.
 
- 
- 
- 
Method Detail- 
toOptionalpublic static <T> java.util.stream.Collector<T,?,java.util.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 be- null) and returns- Optional.empty()if it has none.
- Throws:
- java.lang.IllegalArgumentException- if the stream consists of two or more elements.
- java.lang.NullPointerException- if any element in the stream is- null.
 
 - 
onlyElementpublic static <T extends @Nullable java.lang.Object> java.util.stream.Collector<T,?,T> onlyElement() A collector that takes a stream containing exactly one element and returns that element. The returned collector throws anIllegalArgumentExceptionif the stream consists of two or more elements, and aNoSuchElementExceptionif the stream is empty.
 
- 
 
-