Java 8 Collector that returns a value if there's only a single value [duplicate]
This question already has an answer here: Filter Java Stream to 1 and only 1 element 18 answers I'm a little green on this functional programming and streams stuff, but what little I do know has been very useful! I've had this situation come up several times: List<SomeProperty> distinctProperties = someList.stream() .map(obj -> obj.getSomeProperty()) .distinct() .collect(Collectors.toList()); if (distinctProperties.size() == 1) { SomeProperty commonProperty = distinctProperties.get(0); // take some action knowing that all share this common property } What I really want is: Optional