Normally, getOrDefault
is a way, but if you have several alternative keys, this not only affects readability, but also turns the performance advantage back. With type code:
address.setStreet(map.getOrDefault("STORE_STREET", map.getOrDefault("OFFICE_STREET", ...));
you first look at alternative keys to get the recession value before you even see if a primary key (or a key with a higher priority) is present.
The decision will be
Stream.of("STORE_STREET", "OFFICE_STREET", ...) .map(map::get) .filter(Objects::nonNull) .findFirst() .ifPresent(address::setStreet);
When doing this at a time, its performance may be less than a simple cycle, however, due to the higher initialization costs, the difference in performance will then be irrelevant. For frequent execution there will be no significant difference, so you have to decide based on readability (which, of course, is subjective).
source share