【发布时间】:2021-07-23 22:01:32
【问题描述】:
考虑:
public Map<String, Ethernet> foo() throws SocketException {
var networkInterfaces = NetworkInterface.getNetworkInterfaces();
return Collections.list(networkInterfaces).stream()
.filter(iFace -> **iFace.getName()**.startsWith("w"))
.map(this::getEthernet)
.collect(Collectors.toMap(**iFace.getName()????**, Function.identity()));
}
public Ethernet getEthernet(NetworkInterface networkInterface) {
//return Ethernet
}
如何正确执行 collect 以获取 iFace.getName() 作为键和以太网对象作为每个流元素的值?
【问题讨论】:
-
你的意思是像
NetworkInterface::getName这样的东西吗? (第二种情况)
标签: java java-stream