028-86922220
建站资讯

网站建设资讯

为你提供网站建设行业资讯、网站优化知识、主机域名邮箱、网站开发常见问题等。

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://whjierui.cn/article/hepjs.html

其他资讯