Java8 stream中的toMap、groupBy、sorted、findFirst、findAny、min、max的说明与使用示例
1、toMap:转为Map。
2、groupBy:分组。
3、sorted:排序。
4、findFirst、findAny:查找指定数据。
5、min、max:极值。
使用示例:
package com.cfay.demo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
public class StreamDemo {
public static void main(String[] args) {
List<Student> studentList = buildStudent();
toMap(studentList);
groupBy(studentList);
sorted(studentList);
findFirstOrAny(studentList);
minOrMax(studentList);
}
private static void toMap(List<Student> studentList) {
printTip("toMap");
Map<Long, Student> studentMap = studentList.stream()
.collect(Collectors.toMap(Student::getId, Function.identity()));
studentMap.forEach((index, student) -> System.out.println(student));
dividingLine();
}
private static void groupBy(List<Student> studentList) {
printTip("groupBy");
Map<BigDecimal, List<Student>> scoreMap = studentList.stream().collect(Collectors.groupingBy(Student::getScore));
scoreMap.forEach((index, students) -> students.forEach(System.out::println));
dividingLine();
}
private static void sorted(List<Student> studentList) {
printTip("sorted");
printTip("①按照分数进行升序排列");
printTip("方式一");
List<Student> orderByScoreAsc1 = studentList.stream().sorted(Comparator.comparing(Student::getScore))
.collect(Collectors.toList());
orderByScoreAsc1.forEach(System.out::println);
dividingLine();
printTip("方式二");
List<Student> orderByScoreAsc2 = studentList.stream().sorted(Comparator.comparing(Student::getScore))
.collect(Collectors.toList());
orderByScoreAsc2.forEach(System.out::println);
dividingLine();
printTip("②按照分数进行倒序排列");
printTip("方式一");
List<Student> orderByScoreDesc1 = studentList.stream().sorted(Comparator.comparing(Student::getScore,
Comparator.reverseOrder())).collect(Collectors.toList());
orderByScoreDesc1.forEach(System.out::println);
dividingLine();
printTip("方式二");
List<Student> orderByScoreDesc2 = studentList.stream().sorted(Comparator.comparing(Student::getScore)
.reversed()).collect(Collectors.toList());
orderByScoreDesc2.forEach(System.out::println);
dividingLine();
printTip("多属性排序");
List<Student> moreStudents = studentList.stream().sorted(Comparator.comparing(Student::getId)
.thenComparing(Student::getName).reversed()
.thenComparing(Student::getScore, Comparator.reverseOrder())).collect(Collectors.toList());
moreStudents.forEach(System.out::println);
dividingLine();
}
private static void findFirstOrAny(List<Student> studentList) {
printTip("findFirstOrAny");
Student studentFirst = studentList.stream().filter(stu -> BigDecimal.valueOf(88L).equals(stu.getScore()))
.findFirst().orElseGet(Student::new);
System.out.println("studentFirst = " + studentFirst);
Student studentAny = studentList.stream().filter(stu -> BigDecimal.valueOf(88L).equals(stu.getScore()))
.findFirst().orElseGet(Student::new);
System.out.println("studentAny = " + studentAny);
dividingLine();
}
private static void minOrMax(List<Student> studentList) {
printTip("minOrMax");
Student studentMin = studentList.stream().filter(stu -> BigDecimal.valueOf(88L).equals(stu.getScore()))
.min(Comparator.comparing(Student::getId)).orElse(null);
System.out.println("studentMin = " + studentMin);
Student studentMax = studentList.stream().filter(stu -> BigDecimal.valueOf(88L).equals(stu.getScore()))
.max(Comparator.comparing(Student::getId)).orElse(null);
System.out.println("studentMax = " + studentMax);
dividingLine();
}
public static List<Student> buildStudent() {
List<Student> studentList = new ArrayList<>();
studentList.add(new Student(1L, "22", BigDecimal.valueOf(88L)));
studentList.add(new Student(2L, "33", BigDecimal.valueOf(89L)));
studentList.add(new Student(3L, "44", BigDecimal.valueOf(80L)));
studentList.add(new Student(4L, "55", BigDecimal.valueOf(84L)));
studentList.add(new Student(5L, "66", BigDecimal.valueOf(88L)));
studentList.add(new Student(6L, "77", BigDecimal.valueOf(90L)));
studentList.add(new Student(7L, "88", BigDecimal.valueOf(95L)));
studentList.add(new Student(8L, "99", BigDecimal.valueOf(99L)));
return studentList;
}
private static void dividingLine() {
System.out.println("------------------------------------");
}
private static void printTip(String tip) {
System.out.println(tip + " start ...");
}
@Data
@NoArgsConstructor
@AllArgsConstructor
static class Student {
private Long id;
private String name;
private BigDecimal score;
}
}