List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < 10000000; i++) {
list.add(i);
}
long start = new Date().getTime();
List<String> stringList = list.stream()
.filter(i -> i%2==0)
.map(i -> i.toString())
.collect(Collectors.toList());
long end = new Date().getTime();
System.out.println("lambda used:" + (end - start)); //lambda used:2285ms
start = new Date().getTime();
stringList = new ArrayList<>();
for(Integer i : list){
if(i%2==0){
stringList.add(i.toString());
}
}
end = new Date().getTime();
System.out.println("used:" + (end - start)); // used:1581ms
lambda表达式速度慢了。。。
最新推荐文章于 2024-06-06 06:30:00 发布