我正在将java7代码迁移到Java 8.对于下面在Java 8中发布的代码,可以是等效的(最好是一个衬垫)
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
List<Integer> output = new ArrayList<>(list.size());
for(Integer n : list) {
int x = n * n * n;
output.add(x);
}
Run Code Online (Sandbox Code Playgroud)
你可以得到一个流的list,然后使用map来计算n * n * n,然后收集通过数据采集器
List<Integer> output = list.stream().map(n -> n * n * n).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)