stream 里面的 Collectors.toMap 用法
来源:互联网 发布:索尼淘宝授权店真假 编辑:程序博客网 时间:2024/04/27 19:15
第一步:
package test;public class Person {private Integer id;private String name;public Person(Integer id, String name) {this.id = id;this.name = name;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}}
第二步:
package test;import java.util.ArrayList;import java.util.List;import java.util.Map;import java.util.function.Function;import java.util.stream.Collectors;import java.util.stream.Stream;public class Test {public static void main(String[] args) {List<Person> list = new ArrayList();list.add(new Person(1, "haha"));list.add(new Person(2, "rere"));list.add(new Person(3, "fefe"));Map<Integer, Person> mapp = list.stream().collect(Collectors.toMap(Person::getId, Function.identity()));System.out.println(mapp);System.out.println(mapp.get(1).getName());Map<Integer, String> map = list.stream().collect(Collectors.toMap(Person::getId, Person::getName));System.out.println(map);}}
得到的结果:
{1=test.Person@4b9385, 2=test.Person@1311334, 3=test.Person@2a0b20}
haha
{1=haha, 2=rere, 3=fefe}
阅读全文
0 0
- stream 里面的 Collectors.toMap 用法
- Stream 中Collectors 的用法
- Stream 里面的ifPresent用法
- Stream Collectors groupingBy 例子
- Java8系列--Java Stream进阶篇(collector、collectors、colltect的关系)
- Java 8 Stream Collectors groupingBy 示例
- java 8 Stream Collectors使用注意
- adodb.stream的用法
- Stream的用法
- Collectors
- Collectors
- 关于fileupload里面的Stream ended unexpectedly
- adodb.stream的具体用法
- ADODB.Stream的用法实例
- java8中Collectors的学习
- 关于Node.js里面的Transform Stream类型
- Struts 2 的stream result用法
- Struts2 result type="stream"的用法
- 同一页面tab切换对应显示内容分页实现
- python基础
- 集合进阶2
- ActiveMQ消息中间件Producer和Consumer
- 【每日一题-16】重建二叉树&C模拟实现C++继承多态
- stream 里面的 Collectors.toMap 用法
- Android 中使用 Kotlin 快速入门
- rabbitmq的中流砥柱
- 对象初始化过程
- MySQL修改root用户密码
- CRM的dev(七)--为easyUI的datagrid每行增加一个单选按钮
- 开发监控Linux 内存 Shell 脚本
- 取模运算总结
- java.lang.ClassNotFoundException: org.apache.commons.lang.exception.NestableRuntimeException