lambda表达式速度慢了。。。

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表达式)