1

Consider:

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
}

How can I correctly execute collect to get iFace.getName() as the key and the Ethernet object as the value for each stream element?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Diem
  • 101
  • 4

1 Answers1

1

You need to call getEthernet in collect and not in a map before. Your code can look like this:

return Collections.list(networkInterfaces).stream()
        .filter(iFace -> iFace.getName().startsWith("w"))
        .collect(Collectors.toMap(NetworkInterface::getName, this::getEthernet));
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140