Sometimes the commonly available Collectors are not what we need.
Here is one example for a Singleton Custom Collector:
public static <T> Collector<T, ?, T> singleton() {
return Collectors.collectingAndThen(
Collectors.toList(), list -> {
if (list.size() != 1) {
// or log and return nil
throw new CustomException();
}
return list.get(0);
}
);
}
//and use it:
myObjectToStream.stream()
.filter(elem -> elem.getExampleAttribute() == 0).collect(singleton());