If you don't mind using Guava, you can wrap your code with Iterables.getOnlyElement
, so it would look something like that:
SomeProperty distinctProperty = Iterables.getOnlyElement(
someList.stream()
.map(obj -> obj.getSomeProperty())
.distinct()
.collect(Collectors.toList()));
IllegalArgumentException
will be raised if there is more than one value or no value, there is also a version with default value.