新网创想网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

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://www.wjwzjz.com/article/hepjs.html
在线咨询
服务热线
服务热线:028-86922220
TOP