Skip to content

Instantly share code, notes, and snippets.

@dizzzz
Created July 2, 2025 18:30
Show Gist options
  • Save dizzzz/ff76a2cfaeed7d91e132acfd45bae3bc to your computer and use it in GitHub Desktop.
Save dizzzz/ff76a2cfaeed7d91e132acfd45bae3bc to your computer and use it in GitHub Desktop.
Find loopback address - full lambda
package org.example;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Collections;
import java.util.List;
import java.util.function.Predicate;
public class FullLambdaLoopbackAddresses {
public static void main(final String[] args) {
try {
final Predicate<NetworkInterface> isLoopback = iface -> {
try {
return iface.isLoopback();
} catch (final SocketException e) {
return false;
}
};
final List<String> loopbackAddresses = Collections
.list(NetworkInterface.getNetworkInterfaces())
.stream()
.filter(isLoopback)
.flatMap(networkInterface -> Collections
.list(networkInterface.getInetAddresses())
.stream()
.map(InetAddress::getHostAddress))
.toList();
loopbackAddresses.forEach(System.out::println);
} catch (final SocketException e) {
throw new RuntimeException("Failed to access network interfaces", e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment