日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

stream进行分组统计

發布時間:2023/12/3 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 stream进行分组统计 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
// //groupingBy分組 // Map<Integer, Long> map = houseList.stream().collect(Collectors.groupingBy(House::getBuildId, Collectors.counting())); // //控制臺輸出map // map.forEach((k,v)->{ // System.out.println("k="+k+",v="+v); // }); // // //方式二:使用1.8 stream流 // Map<Object, Integer> sum = houseList.stream().collect(Collectors.groupingBy(House::getBuildId, Collectors.summingInt(p -> ConvertUtil.obj2Int(p.get("count"))))); // sum.forEach((k, v) -> { // newItems.add(ImmutableMap.of("daily", String.valueOf(k), "count", String.valueOf(v))); // });// Map<String,Map<String, Map<String, List<House>>>> result = houseList.stream().collect( // // Collectors.groupingBy(House::getBuildId, // // Collectors.groupingBy(House::getClass, // // Collectors.groupingBy(House::getBuildId))) // // );

Map buildingCount = houseList.stream().collect(Collectors.groupingBy(House::getBuildId));int buildingCountSize = buildingCount.size();

public void testStreamGroupBy() {List<Map<String, Object>> items = Lists.newArrayList();Map<String, Object> map = Maps.newHashMap();map.put("daily", "2018-01-10");map.put("count", 20);items.add(map);map = Maps.newHashMap();map.put("daily", "2018-01-11");map.put("count", 80);items.add(map);map = Maps.newHashMap();map.put("daily", "2018-01-12");map.put("count", 21);items.add(map);map = Maps.newHashMap();map.put("daily", "2018-01-10");map.put("count", 28);items.add(map);final List<Map<String, Object>> newItems = Lists.newArrayList();//方式一: 使用遍歷for (int i = 0; i < result.size(); i++) {Map<String, Object> oldMap = result.get(i);boolean isContain = false;for (int j = 0; j < newList.size(); j++) {Map<String, Object> newMap = newList.get(j);if (newMap.get("daily").equals(oldMap.get("daily"))) {for (String key : oldMap.keySet()) {newMap.put(key, oldMap.get(key));oldMap.put("count", (int) newMap.get("count") + (int) oldMap.get("count"));}isContain = true;break;}}if (!isContain) {newItems .add(oldMap);}}System.out.println("方式一:" + newItems );//方式二:使用1.8 stream流Map<Object, Integer> sum = items.stream().collect(Collectors.groupingBy(m -> m.get("daily"), Collectors.summingInt(p -> ConvertUtil.obj2Int(p.get("count")))));sum.forEach((k, v) -> {newItems.add(ImmutableMap.of("daily", String.valueOf(k), "count", String.valueOf(v)));});System.out.println("方式二:"+newItems);}

java stream分組排序統計求和//java stream多條件分組//其中Student是學生,將學生依次以grade(年級) -> class(班級) -> teacher(任課老師) 分組Map<String,Map<String, Map<String, List<Student>>>> result = students.stream().collect(Collectors.groupingBy(Student::getGrade,Collectors.groupingBy(Student::getClass,Collectors.groupingBy(Student::getTeacher))));//多條件去重students.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getAge() + ";" + o.getName()))), ArrayList::new)).forEach(s -> println(s));//filter過濾students.stream().filter(s -> s.getAge() == 10).forEach(s -> println(s));//sorted排序students.stream().sorted(Comparator.comparing(s-> s.getAge())).forEach(s -> println(s));//1.自然序排序students.stream().sorted();//2.自然序逆序元素,使用Comparator 提供的reverseOrder() 方法students.stream().sorted(Comparator.reverseOrder());//3.按照年齡倒序排序students.stream().sorted(Comparator.comparing(Student::getAge).reversed());//4.不借助stream排序//分數正序排序chineseScores.sort(Comparator.comparing(Integer::intValue));//分數倒序排序chineseScores.sort(Comparator.comparing(Integer::intValue).reversed());//年齡正序排序students.sort(Comparator.comparing(Student::getAge));//年齡倒序排序students.sort(Comparator.comparing(Student::getAge).reversed());//limit方法限制最多返回多少元素students.stream().limit(2).forEach(s -> println(s));//不要前多n個元素,n大于滿足條件的元素個數就返回空的流students.stream().skip(2).forEach(s -> println(s));//最大值 最小值Optional<User> min = students.stream().min(Comparator.comparing(Student::getAge));println(min);Optional<User> max = students.stream().max(Comparator.comparing(Student::getAge));println(max);//轉單集合students.stream().map(Student::getName).forEach(name -> println(name));students.stream().mapToInt(Student::getAge).forEach(age -> println(age));students.stream().mapToDouble(Student::getScoreOfChinese).forEach(scoreOfChinese -> println(scoreOfChinese));students.stream().mapToLong(Student::getAge).forEach(getAge -> println(getAge));//轉單集合求和students.stream().mapToDouble(Student::getScoreOfChinese).sum();//查找匹配指定數據是否存在//allMatch方法與anyMatch差不多,表示所有的元素都滿足才返回true。noneMatch方法表示沒有元素滿足boolean anyMatch = students.stream().anyMatch(s -> s.getAge() == 100);boolean allMatch = students.stream().allMatch(s -> s.getName() == 'hello word');boolean noneMatch = students.stream().noneMatch(s -> s.getStudentId() == '10010');//簡化操作 最大值,最小值,求和Optional<Integer> sum = list.stream().map(User::getAge).reduce(Integer::sum);Optional<Integer> max = list.stream().map(User::getAge).reduce(Integer::max);Optional<Integer> min = list.stream().map(User::getAge).reduce(Integer::min);println(sum);println(max);println(min);//統計IntSummaryStatistics statistics = students.stream().collect(Collectors.summarizingInt(User::getAge));double average = statistics.getAverage();long count = statistics.getCount();int max = statistics.getMax();int min = statistics.getMin();long sum = statistics.getSum();//轉setSet<User> collect = students.stream().collect(Collectors.toSet());Iterator<User> iterator = collect.iterator();while(iterator.hasNext()) {System.out.println(iterator.next().getUserId());}//轉mapMap<String, User> collect = students.stream().collect(Collectors.toMap(Student::getName, s -> s));for (String name : collect.keySet()) {//得到每個key多對用value的值Student s = collect.get(name);println(s);}

總結

以上是生活随笔為你收集整理的stream进行分组统计的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。