Created
July 2, 2025 18:30
-
-
Save dizzzz/ff76a2cfaeed7d91e132acfd45bae3bc to your computer and use it in GitHub Desktop.
Find loopback address - full lambda
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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