目录
1.集合过滤
list.stream().filter(x->条件).collect(Collectors.toList())
示例
List<User> userList= userService.queryAll();
userList = userList.stream().filter(x -> "zhang".equals(x.getUserName())).collect(Collectors.toList());
2.把老集合的字段提取出来重新生成一个新的集合
list.stream().map(条件).collect(Collectors.toList());
示例
List<User> userList= userService.queryAll();
List<String> collect = userList.stream().map(x -> x.getUserName()).collect(Collectors.toList());
3.把List转成Map<String,Object>
list.stream().collect(Collectors.toMap(User::getUsername, Function.identity(), (entity1, entity2) -> entity1));
示例
List<User> list = new ArrayList<>();
User user1 = new User();
user1.setUsername();
user1.setPassword();
list.add(user1);
User user2 = new User();
user2.setUsername();
user2.setPassword();
list.add(user2);
Map<String, User> collect = list.stream().collect(Collectors.toMap(User::getUsername, Function.identity(), (entity1, entity2) -> entity1));
4.把List转成Map<String,List<Object>>
list.stream().collect(Collectors.groupingBy(User::getUsername));
示例
Map<String, List<User>> collect = list.stream().collect(Collectors.groupingBy(User::getUsername));