group by age
Map<Integer,List<Person>> map = personsList.stream().collect(Collectors.groupingBy(Person::getAge));
Map<Integer, DoubleSummaryStatistics> collect = persons.stream().collect(
Collectors.groupingBy(Person::getAge,
Collectors.summarizingDouble(Person::getSalary)));
sum(salary)
group by age
Map collect1 = persons.stream().collect(
Collectors.groupingBy(Person::getAge, Collectors.summingDouble(Person::getSalary)));
整体代码总体如下:
import java.util.*;
import java.util.stream.Collectors;
public class Test {
public static void main(String[] args) {
List<Person> persons = new ArrayList<>();
persons.add(new Person("Brandon", 15, 5000));
persons.add(new Person("Braney", 15, 15000));
persons.add(new Person("Jack", 10, 5000));
persons.add(new Person("Robin", 10, 500000));
persons.add(new Person("Tony", 10, 1400000));
// 只对 age 分组
Map<Integer,List<Person>> map = persons.stream().collect(Collectors.groupingBy(Person::getAge));
for (Map.Entry<Integer, List<Person>> entry : map.entrySet()) {
System.out.println("分组" +entry);
}
System.out.println("=======================");
// 对age分组 然后取所有的聚合结果集
Map<Integer, DoubleSummaryStatistics> collect = persons.stream().collect(
Collectors.groupingBy(Person::getAge,
Collectors.summarizingDouble(Person::getSalary)));
for (Map.Entry<Integer, DoubleSummaryStatistics> entry : collect.entrySet()) {
DoubleSummaryStatistics longSummaryStatistics = entry.getValue();
System.out.println("----------------key----------------" + entry.getKey());
System.out.println("求和:" + longSummaryStatistics.getSum());
System.out.println("求平均" + longSummaryStatistics.getAverage());
System.out.println("求最大:" + longSummaryStatistics.getMax());
System.out.println("求最小:" + longSummaryStatistics.getMin());
System.out.println("求总数:" + longSummaryStatistics.getCount());
}
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
// 对age分组 然后取求和结果集
Map<Integer, Double> collect1 = persons.stream().collect(
Collectors.groupingBy(Person::getAge, Collectors.summingDouble(Person::getSalary)));
System.out.println(collect1);
System.out.println("1");
}
}
结果:
分组10=[Person [name=Jack, age=10, salary=5000.0], Person [name=Robin, age=10, salary=500000.0], Person [name=Tony, age=10, salary=1400000.0]]
分组15=[Person [name=Brandon, age=15, salary=5000.0], Person [name=Braney, age=15, salary=15000.0]]
=======================
----------------key----------------10
求和:1905000.0
求平均635000.0
求最大:1400000.0
求最小:5000.0
求总数:3
----------------key----------------15
求和:20000.0
求平均10000.0
求最大:15000.0
求最小:5000.0
求总数:2
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
{10=1905000.0, 15=20000.0}
1
实体类
/**
* 要聚合的实体类
/
class Person {
private String name;
private int age;
private double salary;
public Person(String name, int age, double salary) {
super();
this.name = name;
this.age = age;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String getNameAndAge() {
return this.getName() + "-" + this.getAge();
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", salary=" + salary
+ "]";
}
}