本篇文章主要记录日常工作中的编程经验,一定不是最好,但一定是当前我知道的较好的方法,持续更新
容器类操作
1.Object类型List转数组
List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
Integer[] arr1 = list.toArray(new Integer[list.size()]);
进一步的,Object类型List转基本类型数组:
int[] arr = list.stream().maptoInt().toArray();
Stream流 操作容器
1. 使用stream处理list 然后收集
List<Integer> list1 = new ArrayList<>();
LIst<String> list2 = list1.stream().filter(()->{
//使用过滤器
if(true){
return true;
}
return false;
}).collect(Collectors.toList());
同理, 可以处理Set、Map等等的容器,可以有其他的规则如flatMap()映射成其他的格式,或者是Collertors的其他处理方法如toSet();