目录
前言
本身我是一个比较偏向少使用Stream的人,因为调试比较不方便。
但是, 不得不说,stream确实会给我们编码带来便捷。
正文
Stream流 其实操作分三大块 :
我今天想分享的是 收集 这part的玩法。
![](https://img-blog.csdnimg.cn/img_convert/7b9adff2701221709d6c66ecb7f53feb.png)
OK,开始结合代码示例一起玩下:
lombok依赖引入,代码简洁一点:
1 2 3 4 5 6 | < dependency > < groupId >org.projectlombok</ groupId > < artifactId >lombok</ artifactId > < version >1.18.20</ version > < scope >compile</ scope > </ dependency > |
准备一个UserDTO.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | /** * @Author: JCccc * @Date: 2022-9-20 01:25 * @Description: */ @Data public class UserDTO { /** * 姓名 */ private String name; /** * 年龄 */ private Integer age; /** * 性别 */ private String sex; /** * 是否有方向 */ private Boolean hasOrientation; } |
准备一个模拟获取List的函数:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | private static List<UserDTO> getUserList() { UserDTO userDTO = new UserDTO(); userDTO.setName( "小冬" ); userDTO.setAge( 18 ); userDTO.setSex( "男" ); userDTO.setHasOrientation( false ); UserDTO userDTO2 = new UserDTO(); userDTO2.setName( "小秋" ); userDTO2.setAge( 30 ); userDTO2.setSex( "男" ); userDTO2.setHasOrientation( true ); UserDTO userDTO3 = new UserDTO(); userDTO3.setName( "春" ); userDTO3.setAge( 18 ); userDTO3.setSex( "女" ); userDTO3.setHasOrientation( true ); List<UserDTO> userList = new ArrayList<>(); userList.add(userDTO); userList.add(userDTO2); userList.add(userDTO3); return userList; } |
第一个小玩法 将集合通过Stream.collect() 转换成其他集合/数组:
现在拿List<UserDTO> 做例子
转成 HashSet<UserDTO> :
1 2 3 | List< UserDTO > userList = getUserList(); Stream< UserDTO > usersStream = userList.stream(); HashSet< UserDTO > usersHashSet = usersStream.collect(Collectors.toCollection(HashSet::new)); |
转成 Set<UserDTO> usersSet :
1 2 3 | List< UserDTO > userList = getUserList(); Stream< UserDTO > usersStream = userList.stream(); Set< UserDTO > usersSet = usersStream.collect(Collectors.toSet()); |
转成 ArrayList<UserDTO> :
1 2 3 | List< UserDTO > userList = getUserList(); Stream< UserDTO > usersStream = userList.stream(); ArrayList< UserDTO > usersArrayList = usersStream.collect(Collectors.toCollection(ArrayList::new)); |
转成 Object[] objects :
1 2 3 | List< UserDTO > userList = getUserList(); Stream< UserDTO > usersStream = userList.stream(); Object[] objects = usersStream.toArray(); |
转成 UserDTO[] users :
1 2 3 4 5 6 | List<UserDTO> userList = getUserList(); Stream<UserDTO> usersStream = userList.stream(); UserDTO[] users = usersStream.toArray(UserDTO[]:: new ); for (UserDTO user : users) { System.out.println(user.toString()); } |
第二个小玩法 聚合(求和、最小、最大、平均值、分组)
找出年龄最大:
stream.max()
写法 1:
1 2 3 4 5 6 7 8 | List<UserDTO> userList = getUserList(); Stream<UserDTO> usersStream = userList.stream(); Optional<UserDTO> maxUserOptional = usersStream.max((s1, s2) -> s1.getAge() - s2.getAge()); if (maxUserOptional.isPresent()) { UserDTO masUser = maxUserOptional.get(); System.out.println(masUser.toString()); } |
写法2:
1 2 3 4 5 6 | List<UserDTO> userList = getUserList(); Stream<UserDTO> usersStream = userList.stream(); Optional<UserDTO> maxUserOptionalNew = usersStream.max(Comparator.comparingInt(UserDTO::getAge)); if (maxUserOptionalNew.isPresent()) { UserDTO masUser = maxUserOptionalNew.get(); System.out.println(masUser.toString()); } |
效果:
![](https://img-blog.csdnimg.cn/img_convert/49176fb217753f1e0abb1b33edc57e1a.png)
输出:
UserDTO(name=小秋, age=30, sex=男, hasOrientation=true)
找出年龄最小:
stream.min()
写法 1:
1 2 3 4 5 | Optional<UserDTO> minUserOptional = usersStream.min(Comparator.comparingInt(UserDTO::getAge)); if (minUserOptional.isPresent()) { UserDTO minUser = minUserOptional.get(); System.out.println(minUser.toString()); } |
写法2:
1 | Optional<UserDTO> min = usersStream.collect(Collectors.minBy((s1, s2) -> s1.getAge() - s2.getAge())); |
求平均值:
1 2 3 | List<UserDTO> userList = getUserList(); Stream<UserDTO> usersStream = userList.stream(); Double avgScore = usersStream.collect(Collectors.averagingInt(UserDTO::getAge)); |
效果:
![](https://img-blog.csdnimg.cn/img_convert/1dd189200430c7803f22493d4bdc2557.png)
求和:
写法1:
1 | Integer reduceAgeSum = usersStream.map(UserDTO::getAge).reduce( 0 , Integer::sum); |
写法2:
1 | int ageSumNew = usersStream.mapToInt(UserDTO::getAge).sum(); |
统计数量:
1 | long countNew = usersStream.count(); |
简单分组:
按照具体年龄分组:
1 2 | //按照具体年龄分组 Map<Integer, List<UserDTO>> ageGroupMap = usersStream.collect(Collectors.groupingBy((UserDTO::getAge))); |
效果:
![](https://img-blog.csdnimg.cn/img_convert/2c1618b3c016b840564e98fb8294afaa.png)
分组过程加写判断逻辑:
1 2 3 4 5 6 7 8 | //按照性别 分为"男"一组 "女"一组 Map<Integer, List<UserDTO>> groupMap = usersStream.collect(Collectors.groupingBy(s -> { if (s.getSex().equals( "男" )) { return 1 ; } else { return 0 ; } })); |
效果:
![](https://img-blog.csdnimg.cn/img_convert/200fa0e91077935acb66ba89f0626202.png)
多级复杂分组:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | //多级分组 // 1.先根据年龄分组 // 2.然后再根据性别分组 Map<Integer, Map<String, Map<Integer, List<UserDTO>>>> moreGroupMap = usersStream.collect(Collectors.groupingBy( //1.KEY(Integer) VALUE (Map<String, Map<Integer, List<UserDTO>>) UserDTO::getAge, Collectors.groupingBy( //2.KEY(String) VALUE (Map<Integer, List<UserDTO>>) UserDTO::getSex, Collectors.groupingBy((userDTO) -> { if (userDTO.getSex().equals( "男" )) { return 1 ; } else { return 0 ; } })))); |
效果:
![](https://img-blog.csdnimg.cn/img_convert/b06a45e58cb86972033c2d5662facbdb.png)
总结
到此这篇关于Java8 Stream教程之collect()技巧的文章就介绍到这了,更多相关Java8 Stream collect()技巧内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!