java.util包包含三個類來收集統(tǒng)計信息:
我們可以使用它們來計算任何數(shù)字數(shù)據(jù)組的摘要統(tǒng)計信息。
以下代碼顯示如何計算多個雙精度值的統(tǒng)計信息。
import java.time.LocalDate; import java.time.Month; import java.util.Arrays; import java.util.DoubleSummaryStatistics; import java.util.List; public class Main { public static void main(String[] args) { DoubleSummaryStatistics stats = new DoubleSummaryStatistics(); stats.accept(100.0); stats.accept(300.0); stats.accept(230.0); stats.accept(532.0); stats.accept(422.0); long count = stats.getCount(); double sum = stats.getSum(); double min = stats.getMin(); double avg = stats.getAverage(); double max = stats.getMax(); System.out.printf( "count=%d, sum=%.2f, min=%.2f, average=%.2f, max=%.2f%n", count, sum, min, max, avg); } }
上面的代碼生成以下結(jié)果。
count=5,sum=1584.00, min=100.00, average=316.80, max=532.00
摘要統(tǒng)計類設計為與流一起使用。
它們包含組合兩個摘要統(tǒng)計信息的combine()方法。
以下代碼顯示如何計算收入總結(jié)統(tǒng)計。
import java.time.LocalDate; import java.time.Month; import java.util.Arrays; import java.util.DoubleSummaryStatistics; import java.util.List; public class Main { public static void main(String[] args) { DoubleSummaryStatistics incomeStats = Employee.persons() .stream() .map(Employee::getIncome) .collect(DoubleSummaryStatistics::new, DoubleSummaryStatistics::accept, DoubleSummaryStatistics::combine); System.out.println(incomeStats); } } class Employee { public static enum Gender { MALE, FEMALE } private long id; private String name; private Gender gender; private LocalDate dob; private double income; public Employee(long id, String name, Gender gender, LocalDate dob, double income) { this.id = id; this.name = name; this.gender = gender; this.dob = dob; this.income = income; } public double getIncome() { return income; } public static List<Employee> persons() { Employee p1 = new Employee(1, "Jake", Gender.MALE, LocalDate.of(1971, Month.JANUARY, 1), 2343.0); Employee p2 = new Employee(2, "Jack", Gender.MALE, LocalDate.of(1972, Month.JULY, 21), 7100.0); Employee p3 = new Employee(3, "Jane", Gender.FEMALE, LocalDate.of(1973, Month.MAY, 29), 5455.0); Employee p4 = new Employee(4, "Jode", Gender.MALE, LocalDate.of(1974, Month.OCTOBER, 16), 1800.0); Employee p5 = new Employee(5, "Jeny", Gender.FEMALE, LocalDate.of(1975, Month.DECEMBER, 13), 1234.0); Employee p6 = new Employee(6, "Jason", Gender.MALE, LocalDate.of(1976, Month.JUNE, 9), 3211.0); List<Employee> persons = Arrays.asList(p1, p2, p3, p4, p5, p6); return persons; } }
上面的代碼生成以下結(jié)果。
Collectors類包含要計算的方法數(shù)字數(shù)據(jù)的特定類型的摘要統(tǒng)計信息。
Collectors類包含方法,如counting(),summingDouble(),summingInt(),summingLong(),averagingDouble(),averagingLong(),averagingInt(),minBy()和maxBy()返回一個收集器來執(zhí)行一種特定類型的匯總計算。
以下代碼顯示如何計算摘要統(tǒng)計信息收入。
import java.time.LocalDate; import java.time.Month; import java.util.Arrays; import java.util.DoubleSummaryStatistics; import java.util.List; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { DoubleSummaryStatistics incomeStats = Employee.persons() .stream() .collect(Collectors.summarizingDouble(Employee::getIncome)); System.out.println(incomeStats); } } class Employee { public static enum Gender { MALE, FEMALE } private long id; private String name; private Gender gender; private LocalDate dob; private double income; public Employee(long id, String name, Gender gender, LocalDate dob, double income) { this.id = id; this.name = name; this.gender = gender; this.dob = dob; this.income = income; } public double getIncome() { return income; } public static List<Employee> persons() { Employee p1 = new Employee(1, "Jake", Gender.MALE, LocalDate.of(1971, Month.JANUARY, 1), 2343.0); Employee p2 = new Employee(2, "Jack", Gender.MALE, LocalDate.of(1972, Month.JULY, 21), 7100.0); Employee p3 = new Employee(3, "Jane", Gender.FEMALE, LocalDate.of(1973, Month.MAY, 29), 5455.0); Employee p4 = new Employee(4, "Jode", Gender.MALE, LocalDate.of(1974, Month.OCTOBER, 16), 1800.0); Employee p5 = new Employee(5, "Jeny", Gender.FEMALE, LocalDate.of(1975, Month.DECEMBER, 13), 1234.0); Employee p6 = new Employee(6, "Jason", Gender.MALE, LocalDate.of(1976, Month.JUNE, 9), 3211.0); List<Employee> persons = Arrays.asList(p1, p2, p3, p4, p5, p6); return persons; } }
上面的代碼生成以下結(jié)果。
更多建議: