I have a list of elements, and want to extract the value of the fields' propery. Problem: all elements should have the same property value.
Can I do better or more elegant than the following?
Set<String> matches = fields.stream().map(f -> f.getField()).collect(Collectors.toSet());
if (matches.size() != 1) throw new IllegalArgumentException("could not match one exact element");
String distrinctVal = matches.iterator().next(); //continue to use the value
Is this possible directly using the stream methods, eg using reduce
?