首页 > 编程知识 正文

∑求和公式的上下标,分组求平均值

时间:2023-05-03 14:26:03 阅读:126345 作者:880

publicclassgroupingby { publicstaticvoidmain (字符串[ ] args ) {ArrayListlist=newArrayList ); list.add(newstudent )、王一(、21、11 )、1、newbigdecimal )、timestamp.value of (local datetime.now ) )。 list.add(newstudent ) )粗心的泥猴桃,22,' 22 ',0,newbigdecimal ), timestamp.valueof ) local datetime.now list.add (new student )、23、33、1、newbigdecimal )、180.1 )、timestamp.vall 0、newbigdecimal )、timestamp.valueof ) local datetime.now list.add (new student )、wjdl )、25、55 )、1、new bigdecid timestamp.valueof ) local datetime.now list.add (new student )、韩六)、26、88 )、0、newbigdecimal )、timestamp.valudent list.add(newstudent )、田七)、27、77 )、1、newbigdecimal )、timestamp.value of (local datetime.now ) )。 list.add(newstudent )、28、88、0、newbigdecimal )和timestamp.value of (local datetime.now ) )。 //list.add(newstudent )、29、null、1、newbigdecimal )、180.1 ); //list.add(newstudent(GXdpw,null,' 99 ',null,null ) ); //list.add(newstudent ('肖11 ',null,',null,null ) ); //list.add(newstudent )、柘十二)、null、null、null、null )//1按性别分组//1.1为null的数据不需要,map collect=list=null (.collect (collectors.grouping by ) student33603360getgender ); //1.2必须保留为null的数据Map extendsSerializable,list collect1=list.stream (.collect (collectors.grouping by (a-{ if ) ) (); //2按性别分组后,按分数分组,=60:A 60:B为null的数据为Map extendsSerializable,map collect2=list.stream (.collect () ,collectors.grouping by (B- { string score=b.get score ); if(score==null({return'b '; } else { return score.com Pareto (' 60 ' )=0?' A': 'B

";}})));//3按照gender和height分组,TreeMap默认按key升序TreeMap key不能为nullTreeMap>collect3 = list.stream().collect(Collectors.groupingBy(a -> {if(a.getGender()== null|| a.getHeight()== null) {return"null";}returna.getGender()+ "->"+ a.getHeight();},TreeMap::new,Collectors.toList()));//统计gender为1和0的人数Map extendsSerializable,Long>collect4 = list.stream().collect(Collectors.groupingBy(a -> {if(a.getGender()== null) {return"mix";}returna.getGender();},counting()));//按gender分组后,统计60为分界的人数Map>collect5 = list.stream().collect(Collectors.groupingBy(//gender分组a -> {if(a.getGender()== null) {return"null";//gender为null}returna.getGender()== 1? "M": "F";},Collectors.groupingBy(//60为分界b -> {String score = b.getScore();if(score == null) {return"null";}else{returnscore.compareTo("60")>= 0? "A": "B";}},counting()//统计)));//统计60分界的男女各多少Map>collect6 = list.stream().filter(a -> StringUtils.isNoneBlank(a.getScore()))//过滤score为""和null的数据.filter(b -> b.getGender()!= null)//过滤gender为null的数据.collect(Collectors.groupingBy(c -> c.getScore().compareTo("60")>= 0? "G": "B",//以60分组Collectors.groupingBy(Student::getGender,//以gender分组counting()//统计)));//统计男女各年龄之和Mapcollect7 = list.stream().filter(a -> a.getAge()!= null|| a.getGender()!= null).collect(Collectors.groupingBy(Student::getGender,Collectors.summingInt(Student::getAge)));//分组元素存储在自定义集合Map>collect8 = list.stream().filter(a -> a.getGender()!= null).collect(Collectors.groupingBy(Student::getGender,Collectors.mapping(Student::getAge,toCollection(TreeSet::new))));//计算男女中最高的分数Mapcollect9 = list.stream().filter(a -> a.getGender()!= null).collect(Collectors.groupingBy(Student::getGender,Collectors.collectingAndThen(Collectors.maxBy(Comparator.comparing(Student::getScore)),Optional::get)));//计算男女中最低的分数Mapcollect10 = list.stream().filter(a -> a.getGender()!= null).collect(Collectors.toMap(Student::getGender,Function.identity(),BinaryOperator.minBy(Comparator.comparing(Student::getScore))));// //计算男女中最高的分数 分数相同去用时最短Mapcollect11 = list.stream().filter(a -> a.getGender()!= null).collect(Collectors.toMap(Student::getGender,//性别Function.identity(),//t->tBinaryOperator.maxBy(Comparator.comparing(Student::getScore)//分数比较.thenComparing(b -> b.getEndTime().getTime()- b.getStartTime().getTime()//分数相同,按时间比较,Comparator.reverseOrder()))//外层用的max,故此处排序取反));//相同的存放在list中TreeMapcollect12 = list.stream().filter(a -> a.getGender()!= null).collect(Collectors.toMap(Student::getGender,Function.identity(),BinaryOperator.maxBy(Comparator.comparing(Student::getScore).thenComparing(b -> b.getEndTime().getTime()- b.getStartTime().getTime(),Comparator.reverseOrder())),TreeMap::new));HashMap>hashMap = newHashMap<>();collect12.forEach((k,v)-> {Map>listMap = list.stream().filter(a ->

k.equals(a.getGender())&& v.getScore().equals(a.getScore())&& (a.getEndTime().getTime()-a.getStartTime().getTime())==(v.getEndTime().getTime()- v.getStartTime().getTime())).collect(Collectors.toMap(Student::getGender,t -> {Liststudents = Collections.singletonList(t);return newArrayList<>(students);},(v1,v2)-> {v2.addAll(v1);returnv2;}));hashMap.putAll(listMap);});//分数最高用时最短Optionalmax = list.stream().max(Comparator

.comparing(Student::getScore).thenComparing(a -> a.getEndTime().getTime()- a.getStartTime().getTime(),Comparator.reverseOrder()));Student student = max.isPresent()? max.get(): null;//最大值 分数 用时Student student1 = list.stream().max(Comparator

.comparing(Student::getScore).thenComparing(a -> a.getEndTime().getTime()- a.getStartTime().getTime(),Comparator.reverseOrder())).orElse(null);//男女各组的平均分数Mapcollect13 = list.stream().filter(a -> a.getGender()!= null).filter(b -> StringUtils.isNoneBlank(b.getScore())).collect(Collectors.groupingBy(Student::getGender,Collectors.averagingDouble(a -> Double.parseDouble(a.getScore()))));System.out.println();}}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。