1、快速将List< Object >转换为Map< Field, Object >
List<CanvasComponentSettings> list = service.queryCanvasComponentSettingList(componentCodeList); Map<String, CanvasComponentSettings> map = list.stream().collect(Collectors.toMap(CanvasComponentSettings::getComponentCode, item -> item));
如果Field中有相同的,使用上面的写法就会报错
List<User> userList = Lists.newArrayList( new User().setId("A").setName("张三"), new User().setId("A").setName("李四"), new User().setId("C").setName("王五") ); Map<String, User> collect = userList.stream().collect(Collectors.toMap(User::getId, Function.identity())); System.out.println(collect);
这时就需要调用第二个重载方法,传入合并函数,如:
userList.stream().collect(Collectors.toMap(User::getId, User::getName, (e1, e2) -> e1 + e2)); // 输出结果: A-> 张三李四 C-> 王五
2、filter过滤
List<String> list = Arrays.asList("aaa", "bbb", "ccc", "ddd", "eee"); list = list.stream().filter(e -> !("aaa".equals(e) || "ccc".equals(e))).collect(Collectors.toList()); list.forEach(e -> { System.out.println(e); });
3、map映射
对原list做某种处理,得到一个新的映射list
List<User> userList = userDao.selectByUserId(user.getId()); List<Long> userIds = userList.stream().map(User::getId).collect(Collectors.toList());
所有元素,按某种规律计算:
List<Integer> num = Arrays.asList(1,2,3,4,5); List<Integer> collect1 = num.stream().map(n -> n * 2).collect(Collectors.toList()); System.out.println(collect1); //[2, 4, 6, 8, 10]
4、peek设置值
// 考生号或身份证号是否必填 List<ExamineInfoVo> fail = examineInfoVos.stream() .filter(e -> StringUtils.isAnyEmpty(e.getExamineNumber(), e.getIdCard())) .peek(e -> e.setReason("考生号或身份证号未正确填写")) .collect(Collectors.toList());
5、collect收集结果为集合
// 把结果收集为List List<String> toList = list.stream().map(Person::getAddress).collect(Collectors.toList()); System.out.println(toList); // 把结果收集为Set(去重) Set<String> toSet = list.stream().map(Person::getAddress).collect(Collectors.toSet()); System.out.println(toSet); // 把结果收集为Map,前面的是key,后面的是value,如果你希望value是具体的某个字段,可以改为toMap(Person::getName, person -> person.getAge()) Map<String, Person> nameToPersonMap = list.stream().collect(Collectors.toMap(Person::getName, person -> person)); System.out.println(nameToPersonMap); // 把结果收集起来,并用指定分隔符拼接 String result = list.stream().map(Person::getAddress).collect(Collectors.joining("~")); System.out.println(result);
分组收集
Map<String, List<Books>> collect = books.stream().collect(Collectors.groupingBy(Books::getAuthor));
多字段分组收集
// GROUP BY address, age Map<String, Map<Integer, List<Person>>> doubleGroupingBy = list.stream() .collect(Collectors.groupingBy(Person::getAddress, Collectors.groupingBy(Person::getAge))); System.out.println(doubleGroupingBy); // 简单来说,就是collect(groupingBy(xx)) 扩展为 collect(groupingBy(xx, groupingBy(yy))),嵌套分组 // 解决了按字段分组、按多个字段分组,我们再考虑一个问题:有时我们分组的条件不是某个字段,而是某个字段是否满足xx条件 // 比如 年龄大于等于18的是成年人,小于18的是未成年人 Map<Boolean, List<Person>> adultsAndTeenagers = list.stream().collect(Collectors.partitioningBy(person -> person.getAge() >= 18)); System.out.println(adultsAndTeenagers);
6、flatMap
flatMap()的作用是对流中的元素进行1对多的转换,然后将结果元素展开为新的流(相当于双层for循环)
1)合并集合
List<Integer> a = Arrays.asList(1, 2, 3); List<Integer> b = Arrays.asList(4, 5, 6); List<Integer> mergeList = Stream.of(a, b).flatMap(e -> e.stream()).collect(Collectors.toList()); System.out.println(mergeList);// [1, 2, 3, 4, 5, 6]
2)过滤集合
3)打散数组集合
7、sorted排序
List<String> list = Arrays.asList("c", "e", "a", "d", "b"); list.stream().sorted().forEach(System.out::println);//abcde
8、distinct去重
Stream<String> stream = Stream.of("know", "is", "know", "noknow", "is", "noknow"); stream.distinct().forEach(System.out::println); // know is noknow
9、anyMatch、allMatch
List<String> list = Arrays.asList("you", "give", "me", "stop"); List<String> list2 = Arrays.asList("me", "me", "me", "me"); boolean result = list.parallelStream().anyMatch(e -> e.equals("me"));//true boolean result2 = list.parallelStream().allMatch(e -> e.equals("me"));//false boolean result3 = list.parallelStream().anyMatch(e -> e.equals("mei"));//false boolean result4 = list2.parallelStream().anyMatch(e -> e.equals("me"));//true boolean result5 = list2.parallelStream().allMatch(e -> e.equals("me"));//true