我有这个需求,作为Apache Commons的用户,这就是我所做的:
IteratorUtils.toList(iterator);
Run Code Online (Sandbox Code Playgroud)
Javadoc:https://commons.apache.org/proper/commons-collections/javadocs/api-3.2.2/org/apache/commons/collections/IteratorUtils.html#toList(java.util.Iterator)
使用Iterator获取每个元素并将其添加到List.
List<String> list = new LinkedList<String>();
while(iter.hasNext()) { // iter is of type Iterator<String>
list.add(iter.next());
}
Run Code Online (Sandbox Code Playgroud)