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:
- 21.0
- Author:
- Louis Wasserman
-
Method Summary
Modifier 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
-
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
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.
-