是否可以使用Java 8流构造更简洁地表达以下逻辑:
public static String getNetworkAddress (String ip, String subnetmask) throws UnknownHostException {
byte[] bIP = InetAddress.getByName(ip).getAddress();
byte[] bSB = InetAddress.getByName(subnetmask).getAddress();
byte[] bNT = new byte[4];
for(int i = 0;i<bIP.length;i++) {
bNT[i] = (byte) (bIP[i] & bSB[i]);
}
return InetAddress.getByAddress(bNT).toString().substring(1);
}
我想将此代码更改为流。
答案 0 :(得分:1)
这样的事情呢?
Arrays.stream(winningPositions) // Stream<int[][]>
.anyMatch(win -> // win is an int[] array
gameState[win[0]] == gameState[win[1]] &&
gameState[win[1]] == gameState[win[2]] &&
gameState[win[0]] != 2
); // anyMatch will stop as soon as match has been found.
尽管与原始方法相比,它可能没有任何真正的优势;