Java:为什么java.util.Collections.list(Enumeration)具有破坏性?

Java: Why is java.util.Collections.list(Enumeration) destructive?

我有一个小程序依赖于从本地网络接口获取一些信息。 然后,我需要添加一些功能,这又需要这些信息。 我不想触及我经过测试和高度使用的方法。因此,在时间的压力下,我决定重用该方法,有效地将 NetworkInterface Enumeration 转换为 ArrayList 两次(这不是我最自豪的编码时刻!)。

经过测试,我发现它并没有真正起作用!

操作顺序:

  1. 我从 OS 中获取 NetworkInterface 枚举一次,进入 class 字段
  2. myMethod 将 NetworkInterface Enumeration 转换为 ArrayList,进行一些分析和 returns 一些信息,做一些事情
  3. myMethod 将 NetworkInterface 枚举转换为 ArrayList,进行一些分析和 returns 一些信息,以做一些事情 else

我注意到,在第二次翻译时,结果为零! 在调试中,我注意到我的枚举有一个字段“i”。 该字段:第一次等于 55(我的 IF 的计数),第二次等于 0...

我提供了一小段代码,为了您的调试乐趣,应该重现它(我正在使用 Java 8):

import java.net.NetworkInterface;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;

public class CollectionsListDemo {
    public static void main(String[] args) {
        ArrayList<NetworkInterface> netFacesArList1;
        ArrayList<NetworkInterface> netFacesArList2;
        try {
            Enumeration<NetworkInterface> netFaces = NetworkInterface.getNetworkInterfaces();
            netFacesArList1 = Collections.list(netFaces);
            netFacesArList2 = Collections.list(netFaces);
        } catch (Exception e) {
            throw new RuntimeException("whatever");
        }

        System.out.println(netFacesArList1.size()); // 55, on my machine
        System.out.println(netFacesArList2.size()); // 0
    }
}

基本上,我想知道的是 Collections.list() 以这种方式工作是否有实际原因......或者它只是一个限制......或者我使用错误?

现在,我只是要把这个翻译拿出来,放到一个单独的 ArrayList 中,我想我可以用不同的方法读取它,而不会记住以前读取的内容...

并不是说Collections.list(Enumeration)是“破坏性的”。就是那个Enumeration(一个过时的版本Iterator) is single-use. Once you iter...enumerate through it, it returns empty and 0 elements left. The same holds true for Iterator, although a ListIterator可以让你向前和向后移动,所以即使迭代到最后它也可以一直“倒带”。

当您考虑假定的用例时,其原因变得更加明显:

Enumeration<NetworkInterface> netFaces = NetworkInterface.getNetworkInterfaces();
while(netFaces.hasMoreElements()) {
    NetworkInterface iface = netFaces.nextElement();
    // do something with iface
}
// Enumeration object is now used up