forEachOrdered 执行结果有序
forEach 执行结果无序
下面代码拿走执行一下就行了
public static void main(String[] args) {
List<Test> list = new ArrayList<>();
for (int i = 0; i < 30; i++) {
Test test = new Test(i);
list.add(test);
}
List<Test> sort = new ArrayList<>();
List<Test> unSort = new ArrayList<>();
list.parallelStream().forEachOrdered(sort::add);
list.parallelStream().forEach(unSort::add);
for (Test test : sort) {
System.out.println(test.age);
}
System.out.println("========");
System.out.println("========");
System.out.println("========");
System.out.println("========");
System.out.println("========");
for (Test test : unSort) {
System.out.println(test.age);
}
}
static class Test {
private String name;
private int age;
public Test(int age) {
this.name = "name" + age;
this.age = age;
}
}