List result = list.parallelStream()
.mapToInt(x -> x)
.map(x -> ++x)
.boxed()
.collect(Collectors.toCollection(ArrayList::new));
2. 过滤处理测试
取出一个随机数列(List<Integer>
)中的大于 200 的元素,并组装为一个新的 List<Integer>
,测试的随机数列容量从 10 - 10000000,跑10次取平均时间;
//stream
List result = list.stream()
.mapToInt(x -> x)
.filter(x -> x > 200)
.boxed()
.collect(Collectors.toCollection(ArrayList::new));
//iterator
List result = new ArrayList<>(list.size());
for(Integer e : list){
if(e > 200){
result.add(e);
}
}
//parallel stream
List result = list.parallelStream()
.mapToInt(x -> x)
.filter(x -> x > 200)
.boxed()
.collect(Collectors.toCollection(ArrayList::new));
3. 自然排序测试
对一个随机数列(List<Integer>
)进行自然排序,并组装为一个新的 List<Integer>
,iterator 使用的是 Collections # sort API(使用归并排序算法实现),测试的随机数列容量从 10 - 10000000,跑10次取平均时间;
//stream
List result = list.stream()
.mapToInt(x->x)
.sorted()
.boxed()
.collect(Collectors.toCollection(ArrayList::ne