Java 流统计

2020-11-20 09:22 更新

Java流 - Java流统计


java.util包包含三个类来收集统计信息:

  • DoubleSummaryStatistics
  • LongSummaryStatistics
  • IntSummaryStatistics

我们可以使用它们来计算任何数字数据组的摘要统计信息。

以下代码显示如何计算多个双精度值的统计信息。

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);
  }
}

上面的代码生成以下结果。

count=5,sum=1584.00, min=100.00, average=316.80, max=532.00

流摘要统计

摘要统计类设计为与流一起使用。

它们包含组合两个摘要统计信息的combine()方法。

以下代码显示如何计算收入总结统计。

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;
  }
}

上面的代码生成以下结果。



数字流摘要统计

Collectors类包含要计算的方法数字数据的特定类型的摘要统计信息。

  • Collectors.summarizingDouble()返回DoubleSummaryStatistics。
  • Collectors.summarizingLong()返回一个LongSummaryStatistics。
  • Collectors.summarizingInt()返回一个IntSummaryStatistics。

Collectors类包含方法,如counting(),summingDouble(),summingInt(),summingLong(),averagingDouble(),averagingLong(),averagingInt(),minBy()和maxBy()返回一个收集器来执行一种特定类型的汇总计算。

以下代码显示如何计算摘要统计信息收入。

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;
  }
}

上面的代码生成以下结果。

以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号