public static void main(String[] args) {
Integer[] qq = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
List<Integer> l = new ArrayList<>();
Collections.addAll(l, qq);
//1
l = l.stream().map(e -> {
return e + 1;
}).collect(Collectors.toList());
//2
l.forEach(integer -> integer++);
}
api的设计是通过语义的不同,让开发者做出更符合自己业务的代码。例如题目中
map
和forEach
的区别:map
是变形操作,即:[0, 1, 2, 3]
=>[false, true, true, true]
,如果要完成这个事情,map
就是最佳选择,符合语义设计,代码如下:forEach
是副作用,如果你只是想针对每一个值做额外的操作,如下,打印一下:那就应该用
forEach
,也更符合他的语义。这是语义化的好处,在程序执行层面没什么特别的对错或者好坏