Apache commons-
collections有一个适配器,可以
Iterator像一样使用
Enumeration。看一下IteratorEnumeration。
使Iterator实例看起来像是Enumeration实例的适配器
简而言之,您可以执行以下操作:
Enumeration enumeration = new IteratorEnumeration(hashMap.keySet().iterator());
或者,如果您(出于某种原因)不想包括commons-collection,则可以自己实现此适配器。这很容易-
只需实现实现
Enumeration,将
Iteratorin
传递给构造函数,然后在每次调用和时
hasMoreElements(),
nextElement()都在基础上调用
hasNext()and
。
next()``Iterator
如果您
Enumeration因某些API合同而被迫使用(我假设是这样),请使用此选项。否则使用
Iterator-这是推荐的选项。



