Class MoreCollectors
java.lang.Object
com.google.common.collect.MoreCollectors
Collectors not present in 
java.util.stream.Collectors that are not otherwise associated
 with a com.google.common type.- Since:
- 33.2.0 (available since 21.0 in guava-jre)
- Author:
- Louis Wasserman
- 
Method SummaryModifier and TypeMethodDescriptionA collector that takes a stream containing exactly one element and returns that element.A collector that converts a stream of zero or one elements to anOptional.
- 
Method Details- 
toOptionalA 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:
- IllegalArgumentException- if the stream consists of two or more elements.
- NullPointerException- if any element in the stream is- null.
 
- 
onlyElementA 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.
 
-