一、Stream 对列表进行去重distinct
distinct()是Java 8 中 Stream 提供的方法,返回的是由该流中不同元素组成的流。distinct()使用 hashCode() 和 eqauls() 方法来获取不同的元素。
因此,需要去重的类必须实现 hashCode() 和 equals() 方法。换句话讲,我们可以通过重写定制的 hashCode() 和 equals() 方法来达到某些特殊需求的去重。
1. 对于 String 列表的去重
因为 String 类已经覆写了 equals() 和 hashCode() 方法,所以可以去重成功。
stringList = stringList.stream().distinct().collect(Collectors.toList());
2. 对于实体类列表的去重
注:代码中我们使用了 Lombok 插件的 @Data注解,可自动覆写 equals() 以及 hashCode() 方法。
studentList = studentList.stream().distinct().collect(Collectors.toList());
3. 根据 List<Object> 中 Object 某个属性去重
// 引入了两个静态方法,以及通过 TreeSet<> 来达到获取不同元素的效果
// 1. import static java.util.stream.Collectors.collectingAndThen;
// 2. import static java.util.stream.Collectors.toCollection;
studentList = studentList.stream().collect(
collectingAndThen(
toCollection(() -> new TreeSet<>(Comparator.comparing(Student::getName))), ArrayList::new)
);
二、lambda使用
List<Student> students = new ArrayList<>(3);
students.add(new Student("路飞", 22, 175));
students.add(new Student("红发", 40, 180));
students.add(new Student("白胡子", 50, 185));
1:按对象属性筛选(筛选出出身高小于180的同学)
List<Student> list = students.stream()
.filter(stu -> stu.getStature() < 180)
.collect(Collectors.toList());
2:获取对象list中某个属性List
List<String> names = students.stream().map(student -> student.getName())
.collect(Collectors.toList());
3:多个list合并
List<Student> studentList = Stream.of(students,
asList(new Student("艾斯", 25, 183),
new Student("雷利", 48, 176)))
.flatMap(students1 -> students1.stream()).collect(Collectors.toList());
4:求最大最小值
Optional<Student> max = students.stream()
.max(Comparator.comparing(stu -> stu.getAge()));
Optional<Student> min = students.stream()
.min(Comparator.comparing(stu -> stu.getAge()));
5:判断是否有值
if (max.isPresent()) {
System.out.println(max.get());
}
if (min.isPresent()) {
System.out.println(min.get());
}
6:统计功能
long count = students.stream().filter(s1 -> s1.getAge() < 45).count();
7:求最大值maxBy、最小值minBy、平均值averagingInt
准备数据:
OutstandingClass ostClass1 = new OutstandingClass("一班", students1);
//复制students1,并移除一个学生
List<Student> students2 = new ArrayList<>(students1);
students2.remove(1);
OutstandingClass ostClass2 = new OutstandingClass("二班", students2);
//将ostClass1、ostClass2转换为Stream
Stream<OutstandingClass> classStream = Stream.of(ostClass1, ostClass2);
OutstandingClass outstandingClass = biggestGroup(classStream);
System.out.println("人数最多的班级是:" + outstandingClass.getName());
System.out.println("一班平均年龄是:" + averageNumberOfStudent(students1));
方法1:获取人数最多的班级
private static OutstandingClass biggestGroup(Stream<OutstandingClass> outstandingClasses) {
return outstandingClasses.collect(
maxBy(comparing(ostClass -> ostClass.getStudents().size())))
.orElseGet(OutstandingClass::new);
}
方法2:计算平均年龄
private static double averageNumberOfStudent(List<Student> students) {
return students.stream().collect(averagingInt(Student::getAge));
}
8:Collectors.partitioningBy将List<Student>分解成两个集合
Map<Boolean, List<Student>> listMap = students.stream().collect(
Collectors.partitioningBy(student -> student.getSpecialities().
contains(SpecialityEnum.SING)));
9:字符串拼接
String names = students.stream()
.map(Student::getName).collect(Collectors.joining(",","[","]"));
//输出结果:[路飞,红发,白胡子]
joining接收三个参数,第一个是分界符,第二个是前缀符,第三个是结束符。也可以不传入参数Collectors.joining(),这样就是直接拼接。
三、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("小冬");
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> 做例子
1:转成 HashSet<UserDTO>
List<UserDTO> userList = getUserList();
Stream<UserDTO> usersStream = userList.stream();
HashSet<UserDTO> usersHashSet = usersStream.collect(Collectors.toCollection(HashSet::new));
2:转成 Set<UserDTO> usersSet
List<UserDTO> userList = getUserList();
Stream<UserDTO> usersStream = userList.stream();
Set<UserDTO> usersSet = usersStream.collect(Collectors.toSet());
3:转成 ArrayList<UserDTO>
List<UserDTO> userList = getUserList();
Stream<UserDTO> usersStream = userList.stream();
ArrayList<UserDTO> usersArrayList = usersStream.collect(Collectors.toCollection(ArrayList::new));
4:转成 Object[] objects
List<UserDTO> userList = getUserList();
Stream<UserDTO> usersStream = userList.stream();
Object[] objects = usersStream.toArray();
5:转成 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());
}
-
二、聚合(求和、最小、最大、平均值、分组)
1:找出年龄最大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());
}
2:找出年龄最小stream.min()
写法 1:
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()));
3:求平均值
List<UserDTO> userList = getUserList();
Stream<UserDTO> usersStream = userList.stream();
Double avgScore = usersStream.collect(Collectors.averagingInt(UserDTO::getAge));
效果:
4:求和
写法1:
Integer reduceAgeSum = usersStream.map(UserDTO::getAge).reduce(0, Integer::sum);
写法2:
int ageSumNew = usersStream.mapToInt(UserDTO::getAge).sum();
5:统计数量
long countNew = usersStream.count();
6:简单分组
按照具体年龄分组:
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;
}
}));
效果:
7:多级复杂分组
//多级分组
// 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;
}
}))));
效果: