Java中如何使用StreamCollectors收集器-创新互联

Java中如何使用Stream Collectors收集器?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

创新互联建站成立与2013年,先为隆子等服务建站,隆子等地企业,进行企业商务咨询服务。为隆子企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

Collectors.toMap:

Student studentA = new Student("20190001","小明");
    Student studentB = new Student("20190002","小红");
    Student studentC = new Student("20190003","小丁");


    //Function.identity() 获取这个对象本身,那么结果就是Map 即 id->student
    //串行收集
   Stream.of(studentA,studentB,studentC)
        .collect(Collectors.toMap(Student::getId,Function.identity()));

    //并发收集
    Stream.of(studentA,studentB,studentC)
        .parallel()
        .collect(Collectors.toConcurrentMap(Student::getId,Function.identity()));

    //================================================================================

    //Map 即 id->name
    //串行收集
    Stream.of(studentA,studentB,studentC)
        .collect(Collectors.toMap(Student::getId,Student::getName));

    //并发收集
    Stream.of(studentA,studentB,studentC)
        .parallel()
        .collect(Collectors.toConcurrentMap(Student::getId,Student::getName));

网站名称:Java中如何使用StreamCollectors收集器-创新互联
网站URL:http://bzwzjz.com/article/hepjs.html

其他资讯

Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号
友情链接: H5网站制作 重庆网站建设 重庆网站制作 外贸网站建设 定制级高端网站建设 成都网站制作 成都网站设计 企业网站设计 网站建设开发 自适应网站设计 手机网站设计 高端网站设计 教育网站设计方案 高端网站设计 LED网站设计方案 网站制作公司 专业网站设计 四川成都网站制作 网站建设 成都网站制作 成都网站设计公司 成都网站制作