內容簡介
本文主要說明在Java8及以上版本中,使用stream().filter()來過濾一個List對象,查找符合條件的對象集合。
list.stream().mapToDouble(User::getHeight).sum()//和 list.stream().mapToDouble(User::getHeight).max()//最大 list.stream().mapToDouble(User::getHeight).min()//最小 list.stream().mapToDouble(User::getHeight).average()//平均值
集合:
List<User> user = new User();
user .stream().collect(Collectors.summingInt(User::getAge))
參數類型:
summarizingDouble 統計數據(double)狀態, 其中包括count min max sum和平均值
summarizingInt 統計數據(int)狀態, 其中包括count min max sum和平均值
summarizingLong 統計數據(long)狀態, 其中包括count min max sum和平均值.
summingInt 求和 返回int類型
summingDouble 求和 返回double類型
summingLong 求和 返回long類型
counting 返回Stream的元素個數
averagingDouble 求平均值 返回double類型
averagingInt 求平均值 返回int類型
averagingLong 求平均值 返回long類型
maxBy 在指定條件下,返回最大值
minBy 在指定條件下,返回最小值
List對象類(StudentInfo)
public class StudentInfo implements Comparable<StudentInfo> {
//名稱
private String name;
//性別 true男 false女
private Boolean gender;
//年齡
private Integer age;
//身高
private Double height;
//出生日期
private LocalDate birthday;
public StudentInfo(String name, Boolean gender, Integer age, Double height, LocalDate birthday){
this.name = name;
this.gender = gender;
this.age = age;
this.height = height;
this.birthday = birthday;
}
public String toString(){
String info = String.format("%s\t\t%s\t\t%s\t\t\t%s\t\t%s",this.name,this.gender.toString(),this.age.toString(),this.height.toString(),birthday.toString());
return info;
}
public static void printStudents(List<StudentInfo> studentInfos){
System.out.println("[姓名]\t\t[性別]\t\t[年齡]\t\t[身高]\t\t[生日]");
System.out.println("----------------------------------------------------------");
studentInfos.forEach(s->System.out.println(s.toString()));
System.out.println(" ");
}
@Override
public int compareTo(StudentInfo ob) {
return this.age.compareTo(ob.getAge());
//return 1;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Boolean getGender() {
return gender;
}
public void setGender(Boolean gender) {
this.gender = gender;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Double getHeight() {
return height;
}
public void setHeight(Double height) {
this.height = height;
}
public LocalDate getBirthday() {
return birthday;
}
public void setBirthday(LocalDate birthday) {
this.birthday = birthday;
}
}
測試數據
//測試數據,請不要糾結數據的嚴謹性
List<StudentInfo> studentList = new ArrayList<>();
studentList.add(new StudentInfo("李小明",true,18,1.76,LocalDate.of(2001,3,23)));
studentList.add(new StudentInfo("張小麗",false,18,1.61,LocalDate.of(2001,6,3)));
studentList.add(new StudentInfo("王大朋",true,19,1.82,LocalDate.of(2000,3,11)));
studentList.add(new StudentInfo("陳小跑",false,17,1.67,LocalDate.of(2002,10,18)));
輸出Students列表
//輸出List StudentInfo.printStudents(studentList);
輸出結果如下圖:

使用filter()過濾List
//查找身高在1.8米及以上的男生 List<StudentInfo> boys = studentList.stream().filter(s->s.getGender() && s.getHeight() >= 1.8).collect(Collectors.toList()); //輸出查找結果 StudentInfo.printStudents(boys);
結果如下圖:

List求和(sum)
package com.gp6.list.sum; import com.gp6.bean.Employee; import com.gp6.list.utils.ListUtil; import java.math.BigDecimal; import java.util.IntSummaryStatistics; import java.util.List; import java.util.LongSummaryStatistics; import java.util.Map; import java.util.stream.Collectors; /** * 列表 求和 * * @author gp6 * @date 2019-07-23 */ public class TestSum { public static void main(String[] args) { List<Employee> employeeList = ListUtil.packEmployeeList(); // 對list中,對年齡求和 Integer ageSum = employeeList.stream().mapToInt(Employee::getAge).sum(); // 121 System.out.println(ageSum); // 對list中,對薪資求和 long salarySum = employeeList.stream().mapToLong(Employee::getSalary).sum(); // 56000 System.out.println(salarySum); // 先將 重量 取出,在計算總和 BigDecimal weightSum = employeeList.stream().map(Employee::getWeight).reduce(BigDecimal.ZERO, BigDecimal::add); // 416.45000000000000994759830064140260219573974609375(精度問題) System.out.println(weightSum); // 根據 性別 分組,再計算出 年齡 信息 Map<Integer, IntSummaryStatistics> map = employeeList.stream().collect(Collectors.groupingBy(Employee::getSex, Collectors.summarizingInt(Employee::getAge)));
// 年齡總和 long ageTmpSum = value.getSum(); // 最大年齡 long ageMax = value.getMax(); // 最小年齡 long ageMin = value.getMin(); // 年齡平均值 
