coding,需要用到 int[ ]转换为List ,每次都有大量for循环重复代码,偶然得知 JDK8 的stream可以一行代码搞定这种转换关系,随记录如下
案例
int [ ] nums = new int [ ] {1,2,3,4,5}; 转换为 List存储
往常做法
int [] nums = new int[]{1,2,3,4,5};
List<Integer> res = new ArrayList<>();
for(int i:nums){
res.add(i);
}
stream 新做法
int [] nums = new int[]{1,2,3,4,5};
List<Integer> res = Arrays.stream(nums).boxed().collect(Collectors.toList());
还有一些其他特性,总结如下


618

被折叠的 条评论
为什么被折叠?



