Iterable转List

Iterable转List

Iterable转List

Iterable<Entity> geted = entityDao.findAll();
List<Entity> list = Lists.newArrays();
geted.forEach(single ->{list.add(single)});

学习了:https://blog.csdn.net/u010003051/article/details/53422741

 

Iterator转List

方式1:
#Apache Commons Collections:
import org.apache.commons.collections.IteratorUtils;
Iterator<Element> myIterator = //some iterator
List<Element> myList=IteratorUtils.toList(myIterator);   
 
 
方式二:
或者自己转换
public static <T> List<T> copyIterator(Iterator<T> iter) {
    List<T> copy = new ArrayList<T>();
    while (iter.hasNext())
        copy.add(iter.next());
    return copy;
}
 
使用方式:
List<String> list = Arrays.asList("1", "2", "3");
Iterator<String> iter = list.iterator();
List<String> copy = copyIterator(iter);
 
方式3:
#Guava
import com.google.common.collect.Lists;
Iterator<Element> myIterator =  //some iterator
List<Element> myList = Lists.newArrayList(myIterator);

学习了:https://blog.csdn.net/u010533843/article/details/62085345

 

上一篇:2021/6/14 魔术方法、可迭代对象、异常处理


下一篇:python高级特性-迭代器