十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
Java中如何使用Stream Collectors收集器?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
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));