如何在 Java 中从 Collectors 创建 IntSummaryStatistics?
首先,我们创建一个列表
List<Employee> emp = Arrays.asList( new Employee("John", "Marketing", 5), new Employee("David", "Operations", 10));
我们有 Employee 类,其中包含员工的姓名、部门和职位。
现在,创建排名的摘要,例如计数、平均值、总和等
IntSummaryStatistics summary = emp .stream() .collect(Collectors.summarizingInt(p -> p.rank));
以下是如何在 Java 中从 Collectors 创建 IntSummaryStatistics 的示例
示例
import java.util.Arrays; import java.util.IntSummaryStatistics; import java.util.List; import java.util.stream.Collectors; public class Demo { public static void main(String[] args) throws Exception { List<Employee> emp = Arrays.asList(new Employee("John", "Marketing", 5), new Employee("David", "Operations", 10)); IntSummaryStatistics summary = emp .stream() .collect(Collectors.summarizingInt(p → p.rank)); System.out.println("Summary Statistics = "+summary); } } class Employee { String name; String department; int rank; Employee(String name, String department, int rank) { this.name = name; this.department = department; this.rank = rank; } }
输出
Summary Statistics = IntSummaryStatistics{count=2, sum=15, min=5, average=7.500000, max=10}
广告