准备数据
ArrayList<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < 100000000; i++) {
list.add(i);
}
1.for循环
for (int i = 0; i < list.size(); i++) {
Integer str = list.get(i);
}
2.for-each循环
for (Integer vo:list
) {
}
3.Lambda
list.forEach(u->{});
迭代器
Iterator<Integer> itr = list.iterator();
while(itr.hasNext()){
Integer str = itr.next();
}
for循环耗时106毫秒,for-each循环耗时103毫秒,Lambda耗时122毫秒,迭代器耗时100毫秒
从结果看出,for循环、for-each循环、迭代器差不多,Lambda耗时最长