Java8对数据处理可谓十分流畅,既不改变数据,又能对数据进行很好的处理,今天给大家演示下,用Java8的Stream如何对数据进行分组统计,排序,求和等
这些方法属于java 8的汇总统计类:
-
getAverage(): 它返回所有接受值的平均值。
-
getCount(): 它计算所有元素的总数。
-
getMax(): 它返回最大值。
-
getMin(): 它返回最小值。
-
getSum(): 它返回所有元素的总和。
1. 统计用户age的最大值,最小值,求和,平均值
@Test
public void testStaff1() {
List<Staff> list = staffService.listStaff();
//1.统计用户age的最大值,最小值,求和,平均值
Map<String, IntSummaryStatistics> collect = list.stream()
.collect(Collectors.groupingBy(Staff::getDeptName, Collectors.summarizingInt(Staff::getAge)));
// 获取用户部门
Set<String> collect1 = list.stream().map(Staff::getDeptName).collect(Collectors.toSet());
// 遍历名字,从map中取出对应用户age的status最大值,最小值,平均值。。。
for (String s1 : collect1) {
IntSummaryStatistics statistics1 = collect.get(s1);
System.out.println("第一个用户所在部门为====" + s1);
System.out.println("**********************************************");
System.out.println("age的个数为===" + statistics1.getCount());
System.out.println("age的最大值为===" + statistics1.getMax());
System.out.println("age的最小值为===" + statistics1.getMin());
System.out.println("age的求和为===" + statistics1.getSum());
System.out.println("age的平均值为===" + statistics1.getAverage());
System.out.println();
}
}
结果如下:
下面几个方法类似:
2.求最大值,最小值
@Test
public void testStaff2() {
List<Staff> list = staffService.listStaff();
//2.求最大值,最小值
System.out.println("list = " + list);
//2.1求最大值
Optional<Staff> max = list.stream()
.max(Comparator.comparing(Staff::getAge));
System.out.println("员工年龄最大值max =" + max.get().getAge());
//2.2求最小值
Optional<Staff> min = list.stream()
.min(Comparator.comparing(Staff::getAge));
System.out.println("员工年龄最小值min = " + min.get().getAge());
//2.1求最大值
Optional<Staff> maxSalary = list.stream()
.max(Comparator.comparing(Staff::getSalary));
System.out.println("员工工资最大值max =" + maxSalary.get().getSalary());
//2.2求最小值
Optional<Staff> minSalary = list.stream()
.min(Comparator.comparing(Staff::getSalary));
System.out.println("员工工资最小值min = " + minSalary.get().getSalary());
}
3.求和
@Test
public void testStaff3() {
List<Staff> list = staffService.listStaff();
//3对某个字段求和并汇总
int sum = list.stream().mapToInt(Staff::getAge).sum();
System.out.println("员工年龄的总和 sum = " + sum);
}
4.求某个字段的平均值
@Test
public void testStaff4() {
List<Staff> list = staffService.listStaff();
//4.求某个字段的平均值
Double collect4 = list.stream().collect(Collectors.averagingInt(Staff::getAge));
System.out.println("员工年龄的平均值为" + collect4);
// 简化后
OptionalDouble average = list.stream().mapToDouble(Staff::getAge).average();
if (average.isPresent()){
System.out.println("员工年龄的average =" + average);
}
}
5.拼接某个字段的值,可以设置前缀,后缀或者分隔符
@Test
public void testStaff5() {
List<Staff> list = staffService.listStaff();
//5.拼接某个字段的值,可以设置前缀,后缀或者分隔符
String collect5 = list.stream().map(Staff::getName).collect(Collectors.joining(",", "我是前缀", "我是后缀"));
System.out.println("员工姓名拼接前后缀后 collect5 = " + collect5);
}
6.根据部门进行分组,并获取汇总人数
@Test
public void testStaff6() {
List<Staff> list = staffService.listStaff();
//6.根据部门进行分组,并获取汇总人数
Map<String, Long> collect6 = list.stream().collect(Collectors.groupingBy(Staff::getDeptName, Collectors.counting()));
for (Map.Entry<String, Long> entry : collect6.entrySet()) {
System.out.println(entry.getKey() + "有" + entry.getValue() + "人;");
}
}
7.根据部门和是否退休进行分组,并汇总人数
@Test
public void testStaff7() {
List<Staff> list = staffService.listStaff();
//7.根据部门和是否退休进行分组,并汇总人数
Map<String, Map<Integer, Long>> collect7 = list.stream().collect(Collectors.groupingBy(Staff::getDeptName, Collectors.groupingBy(Staff::getRetired, Collectors.counting())));
System.out.println("collect7 = " + collect7);
for (Map.Entry<String, Map<Integer, Long>> entry : collect7.entrySet()) {
for (Map.Entry<Integer, Long> entry2 : entry.getValue().entrySet()) {
System.out.println(entry.getKey() + RetiredEnum.translate(entry2.getKey()) + "职工有" + entry2.getValue() + "人");
}
}
}
8.根据部门和是否退休进行分组,并取得每组中年龄最大的人
@Test
public void testStaff8() {
List<Staff> list = staffService.listStaff();
//8. 根据部门和是否退休进行分组,并取得每组中年龄最大的人
Map<String, Map<Integer, Staff>> collect8 = list.stream().collect(
Collectors.groupingBy(Staff::getDeptName,
Collectors.groupingBy(Staff::getRetired,
Collectors.collectingAndThen(
Collectors.maxBy(
Comparator.comparing(Staff::getAge)), Optional::get))));
for (Map.Entry<String, Map<Integer, Staff>> entry : collect8.entrySet()) {
for (Map.Entry<Integer, Staff> entry2 : entry.getValue().entrySet()) {
System.out.println(entry.getKey() + RetiredEnum.translate(entry2.getKey()) + "职工" + entry2.getValue().getName() + "年龄最大,最大年龄为 " + entry2.getValue().getAge());
}
}
}
9.分组排序
@Test
public void testStaff9() {
List<Staff> list = staffService.listStaff();
//9分组排序
Map<String, List<Staff>> map = list.stream().collect(
Collectors.groupingBy(Staff::getDeptName, HashMap::new,
Collectors.collectingAndThen(Collectors.toList(),
//正序
listSort -> listSort.stream().sorted(Comparator.comparing(Staff::getAge))
//倒序
//list -> list.stream().sorted(Comparator.comparing(Student::getScore).reversed())
.collect(Collectors.toList())
)));
for (Map.Entry<String, List<Staff>> entry : map.entrySet()) {
StringBuilder sb = new StringBuilder();
sb.append(entry.getKey() + "有\n【");
for (Staff staff : entry.getValue()) {
sb.append(staff.getName() + "】,age为 " + staff.getAge()+";\n【");
}
sb.append("end】");
System.out.println(sb);
}
}