Java8 新特性之Stream
1、Student
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.elk.log.myTest; public class Student implements Comparable<Student> { /** 学号 */ private long id; private String name; private int age; /** 年级 */ private int grade; /** 专业 */ private String major; /** 学校 */ private String school; // 省略getter和setter public Student(long id, String name, int age, int grade, String major, String school) { this.id = id; this.name = name; this.age = age; this.grade = grade; this.major = major; this.school = school; } public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public int getGrade() { return grade; } public void setGrade(int grade) { this.grade = grade; } public String getMajor() { return major; } public void setMajor(String major) { this.major = major; } public String getSchool() { return school; } public void setSchool(String school) { this.school = school; } @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + ", grade=" + grade + ", major='" + major + '\'' + ", school='" + school + '\'' + '}'; } @Override public int compareTo(Student o) { return (int) (this.id-o.id); } }
2、操作
// 初始化 List<Student> students = new ArrayList<Student>() { { add(new Student(20160001, "孔明", 20, 1, "土木工程", "武汉大学")); add(new Student(20160002, "伯约", 21, 2, "信息安全", "武汉大学")); add(new Student(20160003, "玄德", 22, 3, "经济管理", "武汉大学")); add(new Student(20160004, "云长", 21, 2, "信息安全", "武汉大学")); add(new Student(20161001, "翼德", 21, 2, "机械与自动化", "华中科技大学")); add(new Student(20161002, "元直", 23, 4, "土木工程", "华中科技大学")); add(new Student(20161003, "奉孝", 23, 4, "计算机科学", "华中科技大学")); add(new Student(20162001, "仲谋", 22, 3, "土木工程", "浙江大学")); add(new Student(20162002, "鲁肃", 23, 4, "计算机科学", "浙江大学")); add(new Student(20163001, "丁奉", 24, 5, "土木工程", "南京大学")); add(new Student(20162002, "鲁肃", 23, 4, "计算机科学", "浙江大学")); add(new Student(20163001, "丁奉", 24, 5, "土木工程", "南京大学")); } }; //过滤--->列表钟只含有名字为"孔明" List<Student> whuStudents = students.stream().filter(student -> "孔明".equals(student.getName())).collect(Collectors.toList()); //排序 List<Student> sortStudents = students.stream().sorted(new Comparator<Student>() { @Override public int compare(Student o1, Student o2) { return o1.getGrade()-o2.getGrade(); } }).collect(Collectors.toList()); //将实体对象(Student)映射为字符串(String) List<String> names = students.stream().filter(student -> "土木工程".equals(student.getMajor())).map(Student::getName).collect(Collectors.toList()); //统计分数和 int gradeSum = students.stream().mapToInt(Student::getGrade).sum(); String[] strs = {"java8", "is", "easy", "to", "use"}; //扁平化操作???? List<String[]> distinctStrs = Arrays.stream(strs) .map(str -> str.split("")) // 映射成为Stream<String[]> .distinct() .collect(Collectors.toList()); List<String> distinctStrs1 = Arrays.stream(strs) .map(str -> str.split("")) // 映射成为Stream<String[]> .flatMap(Arrays::stream) // 扁平化为Stream<String> .distinct() .collect(Collectors.toList()); //查询列表对象是否存在某个对象 boolean ret1 = students.stream().allMatch(student -> "鲁肃".equals(student.getName())); //年龄的平均值 Double age = students.stream().collect(Collectors.averagingInt(Student::getAge)); //获取最大的分数对象 Student s = students.stream().max(new Comparator<Student>() { @Override public int compare(Student o1, Student o2) { return o1.getGrade() - o2.getGrade(); } }).get(); //计算机科学年龄总和 int totalAge2 = students.stream() .filter(student -> "计算机科学".equals(student.getMajor())) .map(Student::getAge) .reduce(0, Integer::sum); //list大小 Long l = students.stream().count();
具体可查看:https://blog.csdn.net/anthony_ju/article/details/82078450
本文来自博客园,作者:小白啊小白,Fighting,转载请注明原文链接:https://www.cnblogs.com/ywjfx/p/11906927.html