Improving whoami's answer. Since findFirst()
returns an Optional
, it is
a good practice to check if there is a value.
var optional = pair.keySet().stream().findFirst();
if (!optional.isPresent()) {
return;
}
var key = optional.get();
Also, some commented that finding first key of a HashSet
is unreliable. But sometimes we have HashMap
pairs; i.e. in each map we have one key and one value. In such cases finding the first key of such a pair quickly is convenient.