I have the following code:
Set<String> ips = allowedIpsToDescriptions.keySet();
where allowedIpsToDescriptions is a Map<String, String>
I'm trying to convert Set<String> to Set<InetAddress> and here's my code:
Set<InetAddress> allowedIpsInetAddr = new HashSet<>();
for (String ip : ips) {
InetAddress inetAddress = InetAddress.getByName(ip);
allowedIpsInetAddr.add(inetAddress);
}
Is there a cleaner / more efficient way of doing this using streams perhaps?
I think this is good way to implement but you should handle
nullas expected input to prevent localhost as it is the default value returned bygetHostName. Then ensure that input is pure url without port to preventUnknownHostException.