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)

重复问题,具体回答看: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())
        ));
}