Java8中的Stream流操作性能较慢

笔记

采用List的steam().toArray,比直接for循环性能差很多。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class Test{
public static void main(String[] args){

LinkedList<Integer> list = new LinkedList<>();
for (int i = 0; i < 1000; i++) {
list.add(i);
}

long t1 = System.currentTimeMillis();
list.stream().toArray(Integer[]::new);
long t2 = System.currentTimeMillis();
System.out.println(t2 - t1);

long t3 = System.currentTimeMillis();
Integer[] res = new Integer[list.size()];
int j = 0;
for (Integer i : list) {
res[j++] = i;
}
long t4 = System.currentTimeMillis();
System.out.println(t4 - t3);

}
}

result