partitioningBy分组
| # 根据list⾥⾯进⾏分组,字符串⻓度⼤于4的为⼀组,其他为另外⼀组 |
| |
| public class Main { |
| |
| public static void main(String[] args) throws Exception { |
| List<String> list = Arrays.asList("java", "springboot", "HTML5", "CSS3"); |
| Map<Boolean, List<String>> result = list.stream().collect(partitioningBy(obj -> obj.length() > 4)); |
| System.out.println(result); |
| } |
| |
| } |
group by分组
| # 根据学⽣所在的省份,进⾏分组 |
| |
| public class Main { |
| |
| public static void main(String[] args) throws Exception { |
| List<Student> students = Arrays.asList( |
| new Student("广东", 23), |
| new Student("广东", 24), |
| new Student("广东", 23), |
| new Student("北京", 22), |
| new Student("北京", 20), |
| new Student("北京", 20), |
| new Student("海南", 25)); |
| |
| |
| Map<String,List<Student>> listMap = students.stream().collect(Collectors.groupingBy(obj->obj.getProvince())); |
| |
| listMap.forEach((key,value)->{ |
| System.out.println("======="); |
| System.out.println(key); |
| value.forEach(obj->{ |
| System.out.println(obj.getAge()); |
| }); |
| }); |
| } |
| |
| } |
| |
| |
| class Student { |
| |
| private int age; |
| |
| private String province; |
| |
| public Student(String province, int age) { |
| this.province = province; |
| this.age = age; |
| } |
| |
| public Student() { |
| } |
| |
| public int getAge() { |
| return age; |
| } |
| |
| public void setAge(int age) { |
| this.age = age; |
| } |
| |
| public String getProvince() { |
| return province; |
| } |
| |
| public void setProvince(String province) { |
| this.province = province; |
| } |
| } |
分组统计
| 聚合函数进⾏统计查询,分组后统计个数 |
| Collectors.counting() 统计元素个数 |
| # 统计各个省份的⼈数 |
| |
| public class Main { |
| |
| public static void main(String[] args) throws Exception { |
| List<Student> students = Arrays.asList( |
| new Student("广东", 23), |
| new Student("广东", 24), |
| new Student("广东", 23), |
| new Student("北京", 22), |
| new Student("北京", 20), |
| new Student("北京", 20), |
| new Student("海南", 25)); |
| |
| Map<String,Long> listMap = students.stream().collect(Collectors.groupingBy(obj->obj.getProvince(),Collectors.counting())); |
| listMap.forEach((key,value)->{ |
| System.out.println(key+"人数有"+value); |
| }); |
| } |
| |
| } |
| |
| |
| class Student { |
| |
| private int age; |
| |
| private String province; |
| |
| public Student(String province, int age) { |
| this.province = province; |
| this.age = age; |
| } |
| |
| public Student() { |
| } |
| |
| public int getAge() { |
| return age; |
| } |
| |
| public void setAge(int age) { |
| this.age = age; |
| } |
| |
| public String getProvince() { |
| return province; |
| } |
| |
| public void setProvince(String province) { |
| this.province = province; |
| } |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术
2021-08-25 vue常见错误