java8中的stream流遍历

时间:2022-02-15 03:29:40

比较for循环、迭代器、java8Stream流遍历的不同

 package cnom.test.testUtils;

 import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Random; public class TestCollectionsSort { public static void main(String[] args) throws InterruptedException {
TestCollectionsSort test = new TestCollectionsSort(); List<TestSortModel> list = new ArrayList<TestSortModel>();
TestSortModel model = null;
for (int i = 0; i < 80000; i++) {
model = test.new TestSortModel();
int intId = (int) (Math.random() * 100);
model.setIntId(intId);
model.setName("tes" + i);
list.add(model);
}
// JAVA8 使用Comparator排序
// list.sort(Comparator.comparing(TestSortModel::getIntId,
// Comparator.nullsLast(Comparator.naturalOrder()))); List<String> resList = new ArrayList<String>(); long t1 = System.currentTimeMillis();
for (TestSortModel tsm : list) {
int id = tsm.getIntId();
// Thread.sleep(1);
resList.add(tsm.getName());
}
System.out.println("增强for循环 遍历耗时:" + (System.currentTimeMillis() - t1) + ";list.size()="
+ list.size() + ";list=" + resList.toString());
resList.clear(); long t2 = System.currentTimeMillis();
for (int i = 0; i < list.size(); i++) {
int id = list.get(i).getIntId();
// Thread.sleep(1);
resList.add(list.get(i).getName());
}
System.out.println("for循环 遍历耗时:" + (System.currentTimeMillis() - t2) + ";list.size()="
+ list.size() + ";list=" + resList.toString());
resList.clear(); long t3 = System.currentTimeMillis();
Iterator<TestSortModel> it = list.iterator();
TestSortModel tm = null;
while (it.hasNext()) {
tm = it.next();
int id = tm.getIntId();
// Thread.sleep(1);
resList.add(tm.getName());
}
System.out.println("iterator 遍历耗时:" + (System.currentTimeMillis() - t3) + ";list.size()="
+ list.size() + ";list=" + resList.toString());
resList.clear(); //parallerlStream遍历,适用于处理比较耗时的业务逻辑。
//注意:若在遍历中将元素添加到另一个list,则这个list必须是线程安全的,否则遍历过程中会出现两种情况:
//1.这个list中的元素会有丢失的情况; 2.遍历中会抛出:ArrayIndexOutOfBoundsException
List<TestSortModel> listt = Collections.synchronizedList(new ArrayList<TestSortModel>());
long t4 = System.currentTimeMillis();
list.parallelStream().forEach(tsm -> {//若是 forEachOrdered会保持原来list顺序
int id = tsm.getIntId();
// listt.add(tsm);//输出的listt会有元素丢失,线程安全的list则不会
try {
// Thread.sleep(1);
resList.add(tsm.getName());//输出的resList也会有元素丢失
} catch (Exception e) {
e.printStackTrace();
}
});
System.out
.println("list.parallelStream().forEach() 遍历耗时:" + (System.currentTimeMillis() - t4)
+ ";list.size()=" + resList.size() + ";list=" + resList.toString());
resList.clear(); long t5 = System.currentTimeMillis();
list.stream().forEach(tsm -> {//串行执行,与for循环效率相似,代码精简
int id = tsm.getIntId();
try {
// Thread.sleep(1);
resList.add(tsm.getName());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
});
System.out.println("list.stream().forEach() 遍历耗时:" + (System.currentTimeMillis() - t5)
+ ";list.size()=" + list.size() + ";list=" + resList.toString());
resList.clear(); long t6 = System.currentTimeMillis();
list.forEach(tsm -> {//精简代码,与for循环效率相似
int id = tsm.getIntId();
try {
// Thread.sleep(1);
resList.add(tsm.getName());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
});
System.out.println("list.forEach() 遍历耗时:" + (System.currentTimeMillis() - t6)
+ ";list.size()=" + list.size() + ";list=" + resList.toString());
resList.clear(); /**
* for loop1 增强for循环 遍历耗时:80051
* for loop2 for循环 遍历耗时:80032
* iterator 遍历耗时:80069
* list.parallelStream().forEach() 遍历耗时:20101 多线程环境下,执行耗时的业务逻辑时使用效率更好。
* list.stream().forEach() 遍历耗时:80049 精简代码
* list.forEach() 遍历耗时:80031
*/ } public class TestSortModel implements Serializable { private static final long serialVersionUID = -890909910704287938L; private long longId; private int intId; private String name; public long getLongId() {
return longId;
} public void setLongId(long longId) {
this.longId = longId;
} public int getIntId() {
return intId;
} public void setIntId(int intId) {
this.intId = intId;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} }
}