Java8中Stream之collect()方法大全
下文笔者讲述java8中Stream之collect()方法的功能大全,如下所示
Stream流常见的操作种类
创建、处理、收集例:新建一个实体类
UserDTO.java
@Data public class UserDTO { /** * 姓名 */ private String name; /** * 年龄 */ private Integer age; /** * 性别 */ private String sex; /** * 是否有方向 */ private Boolean hasOrientation; }
生成一个list数据
private static List<UserDTO> getUserList() { UserDTO userDTO = new UserDTO(); userDTO.setName("java265"); userDTO.setAge(21); 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> userList = getUserList(); Stream<UserDTO> usersStream = userList.stream(); HashSet<UserDTO> usersHashSet = usersStream.collect(Collectors.toCollection(HashSet::new)); 转成 Set<UserDTO> usersSet : List<UserDTO> userList = getUserList(); Stream<UserDTO> usersStream = userList.stream(); Set<UserDTO> usersSet = usersStream.collect(Collectors.toSet());
转成ArrayList<UserDTO>
List<UserDTO> userList = getUserList(); Stream<UserDTO> usersStream = userList.stream(); ArrayList<UserDTO> usersArrayList = usersStream.collect(Collectors.toCollection(ArrayList::new));
转成Object[] objects
List<UserDTO> userList = getUserList(); Stream<UserDTO> usersStream = userList.stream(); Object[] objects = usersStream.toArray();
转成 UserDTO[] users
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: 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: 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()); } ------运行以上代码,将输出以下信息------ UserDTO(name=猫猫, age=30, sex=男, hasOrientation=true)找出年龄最小
stream.min() Optional<UserDTO> minUserOptional = usersStream.min(Comparator.comparingInt(UserDTO::getAge)); if (minUserOptional.isPresent()) { UserDTO minUser = minUserOptional.get(); System.out.println(minUser.toString()); } 写法2: Optional<UserDTO> min = usersStream.collect(Collectors.minBy((s1, s2) -> s1.getAge() - s2.getAge()));求平均值
List<UserDTO> userList = getUserList(); Stream<UserDTO> usersStream = userList.stream(); Double avgScore = usersStream.collect(Collectors.averagingInt(UserDTO::getAge));求和
写法1 Integer reduceAgeSum = usersStream.map(UserDTO::getAge).reduce(0, Integer::sum); 写法2: int ageSumNew = usersStream.mapToInt(UserDTO::getAge).sum();统计数量
long countNew = usersStream.count();简单分组
按照具体年龄分组 //按照具体年龄分组 Map<Integer, List<UserDTO>> ageGroupMap = usersStream.collect(Collectors.groupingBy((UserDTO::getAge)));分组过程加写判断逻辑
//按照性别 分为"男"一组 "女"一组 Map<Integer, List<UserDTO>> groupMap = usersStream.collect(Collectors.groupingBy(s -> { if (s.getSex().equals("男")) { return 1; } else { return 0; } }));多级复杂分组
//多级分组 // 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; } }))));
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。