本文介紹了將迭代器轉換為列表的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
給定Iterator
,我們怎樣才能方便地將Iterator
轉換成List
List
對其的操作如get(index)
、add(element)
等
Given Iterator<Element>
, how can we conveniently convert that Iterator
to a List<Element>
, so that we can use List
's operations on it such as get(index)
, add(element)
, etc.
推薦答案
最好使用像 Guava:
import com.google.common.collect.Lists;
Iterator<Element> myIterator = ... //some iterator
List<Element> myList = Lists.newArrayList(myIterator);
另一個番石榴示例:
ImmutableList.copyOf(myIterator);
或Apache Commons Collections:
import org.apache.commons.collections.IteratorUtils;
Iterator<Element> myIterator = ...//some iterator
List<Element> myList = IteratorUtils.toList(myIterator);
這篇關于將迭代器轉換為列表的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!