迭代Java 8中的枚举

问题描述

如果您不喜欢在Collections.list(Enumeration)迭代开始之前将全部内容复制到(临时)列表中的事实,则可以使用简单的实用程序方法来帮助自己:

public static <T> void forEachRemaining(Enumeration<T> e, Consumer<? super T> c) {
  while(e.hasMoreElements()) c.accept(e.nextElement());
}

然后,您可以简单地进行操作forEachRemaining(enumeration, lambda-expression);(注意该import static功能)…

解决方法

是否可以Enumeration使用Lambda表达式进行迭代?以下代码段的Lambda表示形式是什么:

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

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

}

我在其中找不到任何流。