Is it possible to iterate an Enumeration
by using Lambda Expression? What will be the Lambda representation of the following code snippet:
JavaScript
x
Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
while (nets.hasMoreElements()) {
NetworkInterface networkInterface = nets.nextElement();
}
I didn’t find any stream within it.
Advertisement
Answer
In case you don’t like the fact that Collections.list(Enumeration)
copies the entire contents into a (temporary) list before the iteration starts, you can help yourself out with a simple utility method:
JavaScript
public static <T> void forEachRemaining(Enumeration<T> e, Consumer<? super T> c) {
while(e.hasMoreElements()) c.accept(e.nextElement());
}
Then you can simply do forEachRemaining(enumeration, lambda-expression);
(mind the import static
feature)…