public static <T> java.util.stream.Collector<T,?,java.util.Optional<T>> singleOrEmpty()
Collector which traverses a stream and returns either a single element
(if there was only one element) or empty (if there were 0 or more than 1
elements). It traverses the entire stream, even if two elements
have been encountered and the empty return value is now certain.
Implementation credit to Misha: http://stackoverflow.com/a/26812693/1153071
public static <T> java.util.Optional<T> singleOrEmptyShortCircuiting(java.util.stream.Stream<T> stream)
Same behavior as MoreCollectors.singleOrEmpty(), except that it returns
early if it is possible to do so. Unfortunately, it is not possible
to implement early-return behavior using the Collector interface,
so MoreCollectors takes the stream as an argument.
Implementation credit to Thomas Jungblut: http://stackoverflow.com/a/26810932/1153071