Guava provides an onlyElement
Collector
, but only use it if you expect the collection to have exactly one element.
Collection<String> stringCollection = ...;
String string = collection.stream().collect(MoreCollectors.onlyElement())
If you are unsure of how many elements there are, use findFirst
.
Optional<String> optionalString = collection.stream().findFirst();