Tapas Bose Tapas Bose - 18 days ago 7
Java Question

Iterate an Enumeration in Java 8

Is it possible to iterate an

Enumeration
by using Lambda Expression? What will be the Lambda representation of the following code snippet:

Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();

while (nets.hasMoreElements()) {
NetworkInterface networkInterface = nets.nextElement();

}


I didn't find any stream within it.

Answer

(Just because this answer is accepted by OP, it doesn't mean that it is the best one. I suggest reading other answers and picking one depending on situation you are in. Personally I find Hogler's answer nicest because of simplicity and no additional iteration - which happens in mine solution)

You can copy elements from your Enumeration to ArrayList with Collections.list and then use it like

Collections.list(yourEnumeration).forEach(yourAction);
Comments