Java8新特性介绍
过滤集合
List<String> newList = list.stream().filter(item -> item != null).collect(Collectors.toList());
找出并返回集合中重复的元素
ArrayList<Integer> list = new ArrayList<>();
list.add(1);
list.add(1);
list.add(2);
list.add(3);
Set<Integer> collect = list.stream().filter(i -> list.stream().filter(i::equals).count() > 1).collect(Collectors.toSet());
返回对象数组某一元素构成的集合
List<String> list1 = list.stream().map(User::getUsername).collect(Collectors.toList());
根据某一元素去重
//list: 去重数组
//TradeCharacterVO: 对象类型
//getCompanyName: 去重字段
ArrayList<TradeCharacterVO> allOfCompanyListCollect = list.stream().collect(Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(TradeCharacterVO::getCompanyName))), ArrayList::new));
根据集合某个属性分组并存入map
Map<String, List<User>> bageOrderMap = list.stream().collect(Collectors.groupingBy(User::getUsername));
类型转换
List ==》String
String str = list.stream().collect(Collectors.joining(","))
String ==》List
String[] strArray = { "array-a", "array-b" };
List<String> strListNew = new ArrayList<>(Arrays.asList(strArray););
List<String>转List<Integer>
//方法一
List<Integer> intList = strList.stream().map(Integer::parseInt).collect(Collectors.toList());
//方法二
List<Integer> intStream = strList.stream().mapToInt(Integer::parseInt).boxed().collect(Collectors.toList());