Map8Test.java
3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package com.bootdo.test;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.google.common.collect.Lists;
import cn.hutool.core.map.MapUtil;
public class Map8Test {
public static void main(String[] args) {
List<Student> list = Lists.newArrayList();
List<Map<String,Object>> dateMapList = new ArrayList<Map<String,Object>>();
dateMapList.add(new Map8Test().newUserMap("测试", "男", 18));
dateMapList.add(new Map8Test().newUserMap("开发", "男", 20));
dateMapList.add(new Map8Test().newUserMap("运维", "女", 19));
dateMapList.add(new Map8Test().newUserMap("DBA", "女", 22));
dateMapList.add(new Map8Test().newUserMap("运营", "男", 24));
dateMapList.add(new Map8Test().newUserMap("产品", "女", 21));
dateMapList.add(new Map8Test().newUserMap("经理", "女", 25));
dateMapList.add(new Map8Test().newUserMap("产品", "女", 21));
//求性别为男的学生集合
List<Student> l1 = list.stream().filter(student -> student.sex.equals("男")).collect(Collectors.toList());
List<Map<String,Object>> list1 = dateMapList.stream().filter(map -> MapUtil.getStr(map, "sex").equals("男")).collect(Collectors.toList());
//map的key值true为男,false为女的集合
Map<Boolean, List<Student>> map = list.stream().collect(Collectors.partitioningBy(student -> student.getSex().equals("男")));
//求性别为男的学生总岁数
Integer sum = list.stream().filter(student -> student.sex.equals("男")).mapToInt(Student::getAge).sum();
//按性别进行分组统计人数
Map<String, Integer> map9 = list.stream().collect(Collectors.groupingBy(Student::getSex, Collectors.summingInt(p -> 1)));
//判断是否有年龄大于25岁的学生
boolean check = list.stream().anyMatch(student -> student.getAge() > 25);
//获取所有学生的姓名集合
List<String> l2 = list.stream().map(Student::getName).collect(Collectors.toList());
//求所有人的平均年龄
double avg = list.stream().collect(Collectors.averagingInt(Student::getAge));
//求年龄最大的学生
Student s = list.stream().reduce((student, student2) -> student.getAge() > student2.getAge() ? student:student2).get();
Student stu = list.stream().collect(Collectors.maxBy(Comparator.comparing(Student::getAge))).get();
//按照年龄从小到大排序
List<Student> l3 = list.stream().sorted((s1, s2) -> s1.getAge().compareTo(s2.getAge())).collect(Collectors.toList());
//求年龄最小的两个学生
List<Student> l4 = l3.stream().limit(2).collect(Collectors.toList());
//获取所有的名字,组成一条语句
String str = list.stream().map(Student::getName).collect(Collectors.joining(",", "[", "]"));
//获取年龄的最大值、最小值、平均值、求和等等
IntSummaryStatistics intSummaryStatistics = list.stream().mapToInt(Student::getAge).summaryStatistics();
System.out.println(intSummaryStatistics.getMax());
System.out.println(intSummaryStatistics.getCount());
}
private Map newUserMap(String name,String sex,Integer age) {
Map<String,Object> map = new HashMap<String, Object>();
map.put("name", name);
map.put("sex", sex);
map.put("age", age);
return map;
}
}