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收集器-创新互联
本文来源:http://hxwzsj.com/article/hepjs.html

其他资讯

Copyright © 2025 青羊区翔捷宏鑫字牌设计制作工作室(个体工商户) All Rights Reserved 蜀ICP备2025123194号-14
友情链接: 成都网站建设 专业网站设计 营销型网站建设 成都h5网站建设 成都网站建设推广 自适应网站建设 成都网站设计 梓潼网站设计 重庆网站建设 成都网站建设 成都网站建设公司 重庆企业网站建设 网站建设开发 手机网站建设套餐 手机网站制作 成都网站建设 网站建设公司 成都响应式网站建设公司 温江网站设计 自适应网站设计 营销网站建设 网站建设公司