package com.simon.java8;
import java.util.*;
import java.util.stream.Collectors;
public class LambdaTest {
static List<Student> students = new ArrayList<>();
static {
students.add(new Student(1, "Simon", "男", 21, 92));
students.add(new Student(2, "LliLY", "女", 25, 92));
students.add(new Student(3, "LliLY", "女", 25, 100));
students.add(new Student(4, "KL", "男", 28, 80));
students.add(new Student(5, "KL2", "男", 28, 80));
}
public static void main(String args[]) {
System.out.println("=========================================");
//过滤分数大于80的
List<Student> collect = students.stream().filter(x -> x.getScore() > 80)
.sorted(Comparator.comparing(Student::getScore, Comparator.reverseOrder()))
.collect(Collectors.toList());
System.out.println("分数大于80的>>>,分数从高到低");
System.out.println(collect);
System.out.println("=========================================");
System.out.println("去除重复之前");
System.out.println(students);
System.out.println("根据属性id去除重复之后");
List<Student> collect1 = students.stream().collect(Collectors.collectingAndThen
(Collectors.toCollection(() -> new TreeSet<>
(Comparator.comparing(Student::getId))), ArrayList::new));
System.out.println(collect1);
System.out.println("=========================================");
System.out.println("返回前三个");
List<Student> collect2 = students.stream().limit(3).collect(Collectors.toList());
System.out.println(collect2);
System.out.println("=========================================");
System.out.println("返回第2到第4个");
List<Student> collect3 = students.stream().skip(1).limit(3).collect(Collectors.toList());
System.out.println(collect3);
System.out.println("=========================================");
boolean b = students.stream().allMatch(student -> student.getScore() > 90);
System.out.println("是否全部大于90分");
System.out.println(b);
boolean c = students.stream().anyMatch(student -> student.getScore() > 90);
System.out.println("是否有大于90分");
System.out.println(c);
System.out.println("=========================================");
System.out.println("获取大于90分的id集合");
List<Integer> collect4 = students.stream().filter(student -> student.getScore() > 90)
.map(Student::getId).collect(Collectors.toList());
System.out.println(collect4);
System.out.println("=========================================");
System.out.println("计算所有分数的总和方式1");
int i = students.stream().map(Student::getScore).reduce((x, y) -> x + y).get();
System.out.println(i);
System.out.println("计算所有分数的总和方式2");
long count = students.stream().mapToInt(Student::getScore).sum();
System.out.println(count);
System.out.println("=========================================");
System.out.println("按性别进行分组");
Map<String, List<Student>> collect5 = students.stream().
collect(Collectors.groupingBy(Student::getSex));
System.out.println(collect5.size());
System.out.println(collect5.get("男"));
System.out.println(collect5.get("女"));
System.out.println("=========================================");
System.out.println("=========================================");
System.out.println("按性别进行分组,然后根据属性取姓名");
Map<String, List<String>> collect51 = students.stream().
collect(Collectors.groupingBy(Student::getSex, Collectors.mapping(Student::getName, Collectors.toList())));
System.out.println(collect51.size());
System.out.println(collect51.get("男"));
System.out.println(collect51.get("女"));
System.out.println("=========================================");
System.out.println("取分数最高的三位从高到低排列");
List<Student> collect6 = students.stream().sorted(Comparator.
comparing(Student::getScore, Comparator.reverseOrder())).limit(3).collect(Collectors.toList());
System.out.println(collect6);
System.out.println("=========================================");
System.out.println("取分数最高的三位从高到低排列,再根据年龄正序排列");
students.stream().sorted(Comparator.comparing(Student::getScore, Comparator.reverseOrder())
.thenComparing(Student::getAge)).limit(3).collect(Collectors.toList());
System.out.println(collect6);
/*System.out.println("=========================================");
System.out.println("根据姓名分组求总分数::方式1");
System.out.println("select id,name,sex,age,sum(score)as score from student group by name;");
Map<String, Integer> collect7 = students.stream().collect(Collectors.
groupingBy(Student::getName, Collectors.summingInt(Student::getScore)));
Map<String, List<Student>> collect8 = students.stream().collect(Collectors.groupingBy(Student::getName));
List<Student> list = new ArrayList<>();
collect8.keySet().forEach(v -> {
List<Student> list1 = collect8.get(v);
Student student = list1.get(0);
student.setScore(collect7.get(v));
list.add(student);
});
list.forEach(System.out::println);*/
/*System.out.println("=========================================");
System.out.println("根据姓名分组求总分数::方式2");
System.out.println("select id,name,sex,age,sum(score)as score from student group by name;");
Map<String, Student> collect9 = students.stream().
collect(Collectors.toMap(Student::getName, Function.identity(), (m1, m2) -> {
m2.setScore(m2.getScore() + m1.getScore());
return m2;
}));
List<Student> list2 = new ArrayList<>(collect9.values());
list2.forEach(System.out::println);*/
/*
System.out.println("根据姓名分组计算个数,分组后直接count");
System.out.println("select id,name,sex,age,count(1)as score from student group by name;");
Map<String, Integer> collect7 = students.stream().collect(Collectors.
groupingBy(Student::getName, Collectors.counting()));
collect7.forEach(System.out::println);
*/
System.out.println("=========================================");
System.out.println("获取分数最高的学生信息");
Optional<Student> max = students.stream().max(Comparator.comparing(Student::getScore));
System.out.println(max.get());
System.out.println("=========================================");
System.out.println("获取分数最高的学生信息[自定义排序]");
Optional<Student> max1 = students.stream().max(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o1.getScore().compareTo(o2.getScore());
}
});
System.out.println(max1.get());
System.out.println("=========================================");
System.out.println("计算分数大于80的个数");
long count1 = students.stream().filter(x -> x.getScore() > 80).count();
System.out.println(count1);
System.out.println("=========================================");
System.out.println("返回学生姓名全部大写集合");
List<String> collect7 = students.stream().map(x -> x.getName().
toUpperCase()).collect(Collectors.toList());
System.out.println(collect7);
System.out.println("=========================================");
System.out.println("所有学生分数加10,不改变原有集合");
List<Student> collect8 = students.stream().map(student -> {
Student student1 = new Student();
student1.setId(student.getId());
student1.setAge(student.getAge());
student1.setName(student.getName());
student1.setSex(student.getSex());
student1.setScore(student.getScore() + 10);
return student1;
}).collect(Collectors.toList());
System.out.println("第一次改动前:::" + students);
System.out.println("第一次改动后:::" + collect8);
List<Student> collect9 = students.stream().map(student -> {
student.setScore(student.getScore() + 10);
return student;
}).collect(Collectors.toList());
System.out.println("第2次改动前:::" + students);
System.out.println("第2次改动后:::" + collect9);
System.out.println("=========================================");
System.out.println("求分数最大值");
Optional<Integer> reduce = students.stream().map(Student::getScore).reduce((x, y) -> x > y ? x : y);
System.out.println(reduce.get());
System.out.println("=========================================");
System.out.println("求分数总和");
Optional<Integer> reduce2 = students.stream().map(Student::getScore).reduce(Integer::sum);
System.out.println(reduce2.get());
System.out.println("=========================================");
System.out.println("分数大于90的组成map");
Map<Integer, Student> collect10 = students.stream().filter(student -> student.getScore() > 90)
.collect(Collectors.toMap(Student::getId, student -> student));
System.out.println(collect10);
System.out.println("=========================================");
System.out.println("求记录总和");
System.out.println(students.stream().count());
System.out.println(students.stream().collect(Collectors.counting()));
System.out.println("求分数总和");
System.out.println(students.stream().collect(Collectors.summingInt(Student::getScore)));
System.out.println("=========================================");
System.out.println("输出所有姓名");
String collect11 = students.stream().map(Student::getName).collect(Collectors.joining("---"));
System.out.println(collect11);
}
static class Student {
private int id;
private String name;
private String sex;
private int age;
private Integer score;
@Override
public String toString() {
return "{" +
"id=" + id +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
", age=" + age +
", score=" + score +
'}';
}
public Student(int id, String name, String sex, int age, int score) {
this.id = id;
this.name = name;
this.sex = sex;
this.age = age;
this.score = score;
}
public Student() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Integer getScore() {
return score;
}
public void setScore(Integer score) {
this.score = score;
}
}
}