这里放置一些常用的数据类型转换,供自己平时查阅,一直更新
Iterable转List
Iterable<DeviceEntity> iterable= entityRepository.findAll();
List<DeviceEntity> list = new ArrayList<>();
iterable.forEach(single->{list.add(single);}); //关键句
System.out.println(list);
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:
import com.google.common.collect.Lists;
Iterator<Element> myIterator = //some iterator
List<Element> myList = Lists.newArrayList(myIterator);