将两个while循环的代码转换为Java 8流(Stream)代码。

huangapple go评论67阅读模式
英文:

Convert two while loop code to java 8 stream code

问题

以下是您要求的代码的翻译部分:

public static InetAddress getInetAddress() throws SocketException {
    Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
    while (networkInterfaces.hasMoreElements()) {
        NetworkInterface ni = networkInterfaces.nextElement();
        Enumeration<InetAddress> nias = ni.getInetAddresses();
        while (nias.hasMoreElements()) {
            InetAddress ia = nias.nextElement();
            if (!ia.isLinkLocalAddress() && !ia.isLoopbackAddress() && ia instanceof Inet4Address) {
                return ia;
            }
        }
    }
    return null;
}
英文:

I was using simple InetAddress.getLocalHost().getHostAddress() but for one of the server it was giving 127.0.0.0 ip address, which was not expected ip address.
To get actual ip address from server, i am using now below code but want to make it simpler.
Can we make below code simper with java 8 stream code?

public static InetAddress getInetAddress() throws SocketException {
    
    		Enumeration&lt;NetworkInterface&gt; networkInterfaces = NetworkInterface.getNetworkInterfaces();
    		while (networkInterfaces.hasMoreElements()) {
    			NetworkInterface ni = (NetworkInterface) networkInterfaces.nextElement();
    			Enumeration&lt;InetAddress&gt; nias = ni.getInetAddresses();
    			while (nias.hasMoreElements()) {
    				InetAddress ia = (InetAddress) nias.nextElement();
    				if (!ia.isLinkLocalAddress() &amp;&amp; !ia.isLoopbackAddress() &amp;&amp; ia instanceof Inet4Address) {
    					return ia;
    				}
    			}
    		}
    
    		return null;
    	}

答案1

得分: 0

以下是您要翻译的内容:

public static InetAddress getInetAddress() throws SocketException {
    return Collections
            .list(NetworkInterface.getNetworkInterfaces())
            .stream() 
            .flatMap(ni -> Collections.list(ni.getInetAddresses()).stream()) 
            .filter(ia -> !ia.isLinkLocalAddress() && !ia.isLoopbackAddress() && ia instanceof Inet4Address)
            .findFirst() 
            .orElse(null);
}
英文:

It is possible to use Collections.list to convert the enumeration to ArrayList and then use List&#39;s stream and related Stream API functions to filter out unnecessary instances of InetAddress and select the first one matching the criteria:

public static InetAddress getInetAddress() throws SocketException {
    return Collections
            .list(NetworkInterface.getNetworkInterfaces())
            .stream() // Stream&lt;NetworkInterface&gt;
            .flatMap(ni -&gt; Collections.list(ni.getInetAddresses()).stream()) // Stream&lt;InetAddress&gt;
            .filter(ia -&gt; !ia.isLinkLocalAddress() &amp;&amp; !ia.isLoopbackAddress() &amp;&amp; ia instanceof Inet4Address)
            .findFirst()  // Optional&lt;InetAddress&gt;
            .orElse(null);
}

huangapple
  • 本文由 发表于 2020年10月23日 01:54:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/64487915.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定