List<T> 根据对象中的属性处理数据
一、创建测试数据
UserEntity user1 = UserEntity.builder().id(1).name("张三").sex(0).build(); UserEntity user2 = UserEntity.builder().id(2).name("李四").sex(0).build(); UserEntity user3 = UserEntity.builder().id(3).name("张三").sex(0).build(); UserEntity user4 = UserEntity.builder().id(4).name("李四").sex(0).build(); UserEntity user5 = UserEntity.builder().id(5).name("王五").sex(1).build(); List<UserEntity> UserList = new ArrayList<>(); UserList.add(user1); UserList.add(user2); UserList.add(user3); UserList.add(user4); UserList.add(user5); System.out.println("(1)创建测试数据:"+UserList);
二、将测试数据 姓名、性别一致的数据进行过滤
List<UserEntity> UserList2 = UserList.stream().collect( Collectors.collectingAndThen( Collectors.toCollection( () -> new TreeSet<>(Comparator.comparing(p -> p.getName()+ p.getSex()))),ArrayList::new)); System.out.println("(2)将姓名、性别一致的进行过滤:"+UserList2);
三、将测试数据按照ID倒叙进行排序
List<UserEntity> collect = UserList.stream().sorted(Comparator.comparing(UserEntity::getId).reversed()).collect(Collectors.toList());
System.out.println("(3)将数据按照id倒叙进行排序:"+collect);
四、将测试数据与第二步处理后的数据(UserList2)进行姓名、年龄一致的过滤出来,并且将姓名赋值成 "张三丰"
List<UserEntity> result = UserList.stream() .flatMap(x -> UserList2.stream() .filter(y -> x.getName().equals(y.getName()) && x.getSex().equals(y.getSex()))) .peek(y -> { y.setName("张三丰"); }).collect(Collectors.toList()); System.out.println("(4)将姓名.性别一致的在主数据中过滤出来,并且将名称赋值成\"张三丰\":"+result);
五、将测试数据按照Id进行分组
Map<Integer, List<UserEntity>> collect1 = UserList.stream().collect(Collectors.groupingBy(UserEntity::getSex)); System.out.println("(5-1)获取性别是【0】的数据信息"+collect1.get(0)); System.out.println("(5-2)获取性别是【1】的数据信息"+collect1.get(1)); for (Map.Entry<Integer, List<UserEntity>> entry : collect1.entrySet()) { System.out.println("(5-3)将测试数据按照性别进行分组-遍历方法一:-key = " + entry.getKey() + ", value = " + entry.getValue()); } collect1.forEach((k, v) -> System.out.println("(5-4)将测试数据按照性别进行分组-遍历方法二:-key = " + k + ", value = " + v));
六、控制台输出如下:
(1)创建测试数据:[UserEntity(id=1, name=张三, sex=0), UserEntity(id=2, name=李四, sex=0), UserEntity(id=3, name=张三, sex=0), UserEntity(id=4, name=李四, sex=0), UserEntity(id=5, name=王五, sex=1)] (2)将姓名、性别一致的进行过滤:[UserEntity(id=1, name=张三, sex=0), UserEntity(id=2, name=李四, sex=0), UserEntity(id=5, name=王五, sex=1)] (3)将数据按照id倒叙进行排序:[UserEntity(id=5, name=王五, sex=1), UserEntity(id=4, name=李四, sex=0), UserEntity(id=3, name=张三, sex=0), UserEntity(id=2, name=李四, sex=0), UserEntity(id=1, name=张三, sex=0)] (4)将姓名.性别一致的在主数据中过滤出来,并且将名称赋值成"张三丰":[UserEntity(id=1, name=张三丰, sex=0), UserEntity(id=2, name=张三丰, sex=0), UserEntity(id=5, name=张三丰, sex=1)] (5-1)获取性别是【0】的数据信息[UserEntity(id=1, name=张三丰, sex=0), UserEntity(id=2, name=张三丰, sex=0), UserEntity(id=3, name=张三, sex=0), UserEntity(id=4, name=李四, sex=0)] (5-2)获取性别是【1】的数据信息[UserEntity(id=5, name=张三丰, sex=1)] (5-3)将测试数据按照性别进行分组-遍历方法一:-key = 0, value = [UserEntity(id=1, name=张三丰, sex=0), UserEntity(id=2, name=张三丰, sex=0), UserEntity(id=3, name=张三, sex=0), UserEntity(id=4, name=李四, sex=0)] (5-3)将测试数据按照性别进行分组-遍历方法一:-key = 1, value = [UserEntity(id=5, name=张三丰, sex=1)] (5-4)将测试数据按照性别进行分组-遍历方法二:-key = 0, value = [UserEntity(id=1, name=张三丰, sex=0), UserEntity(id=2, name=张三丰, sex=0), UserEntity(id=3, name=张三, sex=0), UserEntity(id=4, name=李四, sex=0)] (5-4)将测试数据按照性别进行分组-遍历方法二:-key = 1, value = [UserEntity(id=5, name=张三丰, sex=1)]
古今成大事者,不唯有超世之才,必有坚韧不拔之志!