1、基礎類
package com.my.test; public class Student { // 名稱 private String name; // 性別 private String gender; // 年齡 private Integer age; public Student() { } public Student(String name, String gender, Integer age) { this.name = name; this.gender = gender; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", gender='" + gender + '\'' + ", age=" + age + '}'; } }
2、測試類
package com.my.test; import java.util.ArrayList; import java.util.IntSummaryStatistics; import java.util.List; import java.util.Map; import java.util.stream.Collectors; import static java.util.stream.Collectors.averagingInt; import static java.util.stream.Collectors.partitioningBy; public class ListStream { public static void main(String[] args) { // 測試數據 List<Student> list = new ArrayList<>(); list.add(new Student("李明", "男", 18)); list.add(new Student("李飛", "男", 17)); list.add(new Student("張麗", "女", 17)); list.add(new Student("張艷", "女", 20)); list.add(new Student("王朋", "男", 18)); list.add(new Student("陳晴", "女", 18)); list.add(new Student("王菲", "女", 19)); list.add(new Student("李雷", "男", 20)); // 查找性別為男的學生 List<Student> boys = list.stream().filter(s -> "男".equals(s.getGender())).collect(Collectors.toList()); System.out.println("1、查找性別為男的學生:"); System.out.println(boys.toString()); // 查找性別為女的學生 List<Student> girls = list.stream().filter(s -> "女".equals(s.getGender())).collect(Collectors.toList()); System.out.println("2、查找性別為女的學生:"); System.out.println(girls.toString()); // map的key值true為男,false為女的集合 Map<Boolean, List<Student>> map1 = list.stream().collect(partitioningBy(student -> "男".equals(student.getGender()))); System.out.println("3、map的key值true為男,false為女的集合:"); System.out.println(map1); // 求性別為男的學生總歲數 int sum = list.stream().filter(s -> "男".equals(s.getGender())).mapToInt(Student::getAge).sum(); System.out.println("4、求性別為男的學生總歲數:"); System.out.println(sum); // 按性別進行分組統計人數 Map<String, Integer> map2 = list.stream().collect(Collectors.groupingBy(Student::getGender, Collectors.summingInt(p -> 1))); System.out.println("5、按性別進行分組統計人數:"); System.out.println(map2); // 按性別進行分組統計各組 Map<String, List<Student>> map3 = list.stream().collect(Collectors.groupingBy(Student::getGender)); System.out.println("6、按性別進行分組統計各組:"); System.out.println(map3); // 判斷是否有年齡大於25歲的學生 boolean check = list.stream().anyMatch(student -> student.getAge() > 25); System.out.println("7、判斷是否有年齡大於25歲的學生:"); System.out.println(check); // 獲取所有學生的姓名集合 List<String> list2 = list.stream().map(Student::getName).collect(Collectors.toList()); System.out.println("8、獲取所有學生的姓名集合:"); System.out.println(list2); // 求所有人的平均年齡 double avg = list.stream().collect(averagingInt(Student::getAge)); System.out.println("9、求所有人的平均年齡:"); System.out.println(avg); // 求年齡最大的學生 Student s = list.stream().reduce((student, student2) -> student.getAge() > student2.getAge() ? student : student2).get(); System.out.println("10、求年齡最大的學生:"); System.out.println(s); // 按照年齡從小到大排序 List<Student> list3 = list.stream().sorted((s1, s2) -> s1.getAge().compareTo(s2.getAge())).collect(Collectors.toList()); System.out.println("11、按照年齡從小到大排序:"); System.out.println(list3); // 獲取所有的名字,輸出一個字符串 String str = list.stream().map(Student::getName).collect(Collectors.joining(",", "[", "]")); System.out.println("12、獲取所有的名字,輸出一個字符串:"); System.out.println(str); // 獲取年齡的最大值、最小值、平均值、求和 IntSummaryStatistics intSummaryStatistics = list.stream().mapToInt(Student::getAge).summaryStatistics(); System.out.println("13、獲取年齡的最大值、最小值、平均值、求和:"); System.out.println(intSummaryStatistics); } }
