Java中如何将迭代器转换为列表呢?
下文笔者讲述迭代器转换为列表的方法及示例分享,如下所示
使用Apache Commons Collections包 中的 IteratorUtils.tolist 方法 或 com.google.common.collect.Lists包中的 Lists.newArrayList 方法例:
import com.google.common.collect.Lists; Iterator<Element> myIterator = ... //some iterator List<Element> myList = Lists.newArrayList(myIterator); Guava 示例 ImmutableList.copyOf(myIterator); 或Apache Commons Collections : import org.apache.commons.collections.IteratorUtils; Iterator<Element> myIterator = ...//some iterator List<Element> myList = IteratorUtils.toList(myIterator);
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。