Starting with this code:
public Thing thereCanBeOnlyOne(Stream<Thing> stream) {
List<Thing> things = stream.collect(Collectors.toList());
if(things.size() != 1) {
throw new IllegalArgumentException();
}
return things.get(0);
}
Is there a more succint way to express the method body?
What I tried so far:
I read the documentation for Collector
and reduce
, but didn't find anything.