BIGE_BIGGER 2018-10-30 09:56 采纳率: 0%
浏览 1210
已结题

java 统计集合中对象的不特定字段的平均值

java 统计集合中对象的不特定字段的平均值如何做?

try {
String methodName = "get"+projectName;
Method method = EsFitnessTestRecordDO.class.getMethod(methodName);
double ave = projectAndSexList.stream().mapToDouble(method).average().average().getAsDouble();//动态统计,报错,怎么写?
double average = projectAndSexList.stream().mapToDouble(EsFitnessTestRecordDO::getBmi).average().getAsDouble();//普通统计bmi字段的平均值,getBmi是EsFitnessTestRecordDO对象中bmi字段的get方法

    }catch (NoSuchMethodException e){

    }
            ![图图片说明](https://img-ask.csdn.net/upload/201810/30/1540893458_171600.png)
  • 写回答

1条回答 默认 最新

  • threenewbee 2018-10-30 10:40
    关注

    重复问题,具体回答看:https://ask.csdn.net/questions/705290

     private static Map<List<String>, List<MyClass>> groupListBy(List<MyClass> data, String[] groupByFieldNames) {
        final MethodHandles.Lookup lookup = MethodHandles.lookup();
        List<MethodHandle> handles = 
            Arrays.stream(groupByFieldNames)
                  .map(field -> {
                      try {
                          return lookup.findGetter(MyClass.class, field, String.class);
                      } catch (Exception e) {
                          throw new RuntimeException(e);
                      }
                  }).collect(toList());
        return data.stream().collect(groupingBy(
                d -> handles.stream()
                            .map(handle -> {
                                try {
                                    return (String) handle.invokeExact(d);
                                } catch (Throwable e) {
                                    throw new RuntimeException(e);
                                }
                            }).collect(toList())
            ));
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝