Collectors.groupingBy 使用
2017-06-19 20:59
543 查看
Collectors.groupingBy 使用
本文主要介绍一下lambda表达式中的Collectors.groupingBy的使用。//groupingBy使用 System.out.println("=======groupingBy=========="); Stream<Person> stream = Stream.of(new Person("1", "aa", "12"), new Person("1", "bb", "13"), new Person("3", "cc", "14")); System.out.println(stream.collect(Collectors.groupingBy(x -> x.id))); //groupingBy Map<String, List<Person>> tempMap = Stream.of(new Person("1", "aa", "12"), new Person("1", "bb", "13"), new Person("3", "cc", "14")) .collect(Collectors.groupingBy(x -> x.id)); for (String s : tempMap.keySet()) { tempMap.get(s).stream().forEach(x -> System.out.println(x)); } Map<Boolean, List<Integer>> collectGroup = Stream.of(1, 2, 3, 4) .collect(Collectors.groupingBy(it -> it > 3)); System.out.println("collectGroup : " + collectGroup);
这里用到的Person类的代码:
class Person { String id; String name; String age; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public Person() { } public Person(String id, String name, String age) { this.id = id; this.name = name; this.age = age; } @Override public String toString() { return "Person{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", age='" + age + '\'' + '}'; } }
看一下运行结果:
相关文章推荐
- Java 8 Stream Collectors groupingBy 示例
- group by grouping sets的使用
- Java 8 Collectors: groupingBy Example
- java8特性:Collectors.groupingBy进行分组、排序等操作
- 在查询结果后的最后一行加上合计,不用 union all,使用GROUP BY ROLLUP grouping
- Stream Collectors groupingBy 例子
- java8特性:Collectors.groupingBy进行分组、排序等操作 (二)
- XHTML 使用手记 By Stabx
- cmd 方式使用 Search and Replace 例 By shawl.qiu
- 文本编辑软件正则表达式使用笔记 By Stabx
- 如何使用 @ OutputCache 指令的 VaryByCustom 属性来缓存不同版本的页面(二)
- ASP Class(类) 之 使用内建组件 cdo.message 发送邮件 By shawl.qiu
- Enterprise Library Step By Step系列(十六):使用AppSetting Application Block
- ASP + mssql 与 查询分析器 之 使用存储过程 建,添,更,选,删 基本数据操作 By shawl.qiu
- CSS 2.1 使用手记 By Stabx
- subversion客户端的基本使用方法(By Robinvane Suen)
- ASP 技巧一则之 简化创建关闭记录集对象并创建使用简单的MSSQL存储过程 By shawl.qiu
- 使用CallByName“代替”模式窗口。
- 如何使用 @ OutputCache 指令的 VaryByCustom 属性来缓存不同版本的页面
- window.resizeBy()方法使用示例