大佬教程收集整理的这篇文章主要介绍了Collector 工具库:Collectors,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
student s1 = new student("aa", 10,1); student s2 = new student("bb", 20,2); student s3 = new student("cc", 10,3); List<student> list = Arrays.asList(s1, s2, s3); //装成list List<Integer> ageList = list.stream().map(student::getAgE).collect(Collectors.toList()); // [10, 20, 10] //转成set Set<Integer> ageSet = list.stream().map(student::getAgE).collect(Collectors.toSet()); // [20, 10] //转成map,注:key不能相同,否则报错 Map<String, Integer> studentMap = list.stream().collect(Collectors.toMap(student::getName, student::getAgE)); // {Cc=10, bb=20, aa=10} //字符串分隔符连接 String joinName = list.stream().map(student::getName).collect(Collectors.joining(",", "(", ")")); // (aa,bb,cC) //聚合操作 //1.学生总数 Long count = list.stream().collect(Collectors.counTing()); // 3 //2.最大年龄 (最小的minBy同理) Integer maxAge = list.stream().map(student::getAgE).collect(Collectors.maxBy(Integer::comparE)).get(); // 20 //3.所有人的年龄 Integer sumAge = list.stream().collect(Collectors.summingInt(student::getAgE)); // 40 //4.平均年龄 Double averageAge = list.stream().collect(Collectors.averagingDouble(student::getAgE)); // 13.333333333333334 // 带上以上所有方法 DoubleSumMaryStatistics statistics = list.stream().collect(Collectors.summarizingDouble(student::getAgE)); System.out.println("count:" + statistics.getCount() + ",max:" + statistics.getMax() + ",sum:" + statistics.getsum() + ",average:" + statistics.getAverage()); //分组 Map<Integer, List<student>> ageMap = list.stream().collect(Collectors.groupingBy(student::getAgE)); //多重分组,先根据类型分再根据年龄分 Map<Integer, Map<Integer, List<student>>> typeAgeMap = list.stream().collect(Collectors.groupingBy(student::getType, Collectors.groupingBy(student::getAgE))); //分区 //分成两部分,一部分大于10岁,一部分小于等于10岁 Map<Boolean, List<student>> partMap = list.stream().collect(Collectors.partitioningBy(v -> v.getAge() > 10)); //规约 Integer allAge = list.stream().map(student::getAgE).collect(Collectors.reducing(Integer::sum)).get(); //40
以上是大佬教程为你收集整理的Collector 工具库:Collectors全部内容,希望文章能够帮你解决Collector 工具库:Collectors所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。