前言:Java有很多种循环方式,可根据自己的业务需求进行选择
1.for循环(已知循环多少次)
for (int i = 0; i < 100; i++) {
System.out.println(i);
}
2.foreach
weChatUserList.forEach(openId->{ System.out.println(openId); });
3.stream().forEach()
weChatUserList.stream().forEach(openId->{
System.out.println(openId);
});
4.parallelStream().foreach()
weChatUserList.parallelStream().forEach(openId->{
System.out.println(openId);
});
5.forEachOrdered().foreach()
weChatUserList.forEachOrdered().forEach(openId->{
System.out.println(openId);
});
6.forEachOrdered().foreach()与parallelStream().foreach()区别?
forEachOrdered:是通过多线程并行的方式来执行我们的代码
parallelStream:是采用多线程,但由于加入了顺序执行约束,故程序是采用多线程同步的方式运行的