- if (secondHostnames != null) {
- // If one IP maps to multiple hostnames, we concatenate the hostnames (separated by a delimiter)
- secondSrc = secondHostnames.stream().collect(Collectors.joining(delimiter));
- }
+ // If one IP maps to multiple hostnames, we concatenate the hostnames (separated by a delimiter)
+ Optional<String> hostnames = secondHostnames.map(hostnameSet -> hostnameSet.stream().collect(Collectors.joining(delimiter)));
+ // Fall back to IP if we couldn't second pair is present, but we couldn't map to (a) hostname(s).
+ secondSrc = hostnames.isPresent() ? hostnames : secondSrc;