本身我是一个比较偏向少使用Stream的人,因为调试比较不方便。
但是, 不得不说,stream确实会给我们编码带来便捷。
所以还是忍不住想分享一些奇技淫巧。
Stream流 其实操作分三大块 :
创建
处理
收集
我今天想分享的是 收集 这part的玩法。
OK,开始结合代码示例一起玩下:
lombok依赖引入,代码简洁一点:
org.projectlombok
lombok
1.18.20
compile
准备一个UserDTO.java
/**
* @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的函数:
private static List 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 userList = new ArrayList<>();
userList.add(userDTO);
userList.add(userDTO2);
userList.add(userDTO3);
return userList;
}
现在拿List
转成 HashSet
List userList = getUserList();
Stream usersStream = userList.stream();
HashSet usersHashSet = usersStream.collect(Collectors.toCollection(HashSet::new));
转成 Set
List userList = getUserList();
Stream usersStream = userList.stream();
Set usersSet = usersStream.collect(Collectors.toSet());
转成 ArrayList
List userList = getUserList();
Stream usersStream = userList.stream();
ArrayList usersArrayList = usersStream.collect(Collectors.toCollection(ArrayList::new));
转成 Object[] objects :
List userList = getUserList();
Stream usersStream = userList.stream();
Object[] objects = usersStream.toArray();
转成 UserDTO[] users :
List userList = getUserList();
Stream usersStream = userList.stream();
UserDTO[] users = usersStream.toArray(UserDTO[]::new);
for (UserDTO user : users) {
System.out.println(user.toString());
}
stream.max()
写法 1:
ListuserList = getUserList(); Stream usersStream = userList.stream(); Optional maxUserOptional = usersStream.max((s1, s2) -> s1.getAge() - s2.getAge()); if (maxUserOptional.isPresent()) { UserDTO masUser = maxUserOptional.get(); System.out.println(masUser.toString()); }
写法2:
ListuserList = getUserList(); Stream usersStream = userList.stream(); Optional 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()
写法 1:
OptionalminUserOptional = usersStream.min(Comparator.comparingInt(UserDTO::getAge)); if (minUserOptional.isPresent()) { UserDTO minUser = minUserOptional.get(); System.out.println(minUser.toString()); }
写法2:
Optionalmin = usersStream.collect(Collectors.minBy((s1, s2) -> s1.getAge() - s2.getAge()));
ListuserList = getUserList(); Stream 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> ageGroupMap = usersStream.collect(Collectors.groupingBy((UserDTO::getAge)));
效果:
分组过程加写判断逻辑:
//按照性别 分为"男"一组 "女"一组 Map> groupMap = usersStream.collect(Collectors.groupingBy(s -> { if (s.getSex().equals("男")) { return 1; } else { return 0; } }));
效果:
//多级分组 // 1.先根据年龄分组 // 2.然后再根据性别分组 Map>>> moreGroupMap = usersStream.collect(Collectors.groupingBy( //1.KEY(Integer) VALUE (Map >) UserDTO::getAge, Collectors.groupingBy( //2.KEY(String) VALUE (Map >) UserDTO::getSex, Collectors.groupingBy((userDTO) -> { if (userDTO.getSex().equals("男")) { return 1; } else { return 0; } }))));
效果: