类似的问题以前已经回答过,但是我仍然无法弄清楚我的分组和平均方法有什么问题。
[我尝试了多个返回值组合,例如Map<Long, Double>
,Map<Long, List<Double>
,Map<Long, Map<Long, Double>>
,Map<Long, Map<Long, List<Double>>>
,但这些都不能解决IntelliJ抛出的错误:'无法从静态上下文中引用非静态方法' 。此刻,我觉得我只是在盲目猜测。那么,谁能给我一些关于如何确定正确的回报类型的见解?谢谢!
方法:
public static <T> Map<Long, Double> findAverageInEpochGroup(List<Answer> values, ToIntFunction<? super T> fn) {
return values.stream()
.collect(Collectors.groupingBy(Answer::getCreation_date, Collectors.averagingInt(fn)));
}
答案类别:
@Getter
@Setter
@Builder
public class Answer {
private int view_count;
private int answer_count;
private int score;
private long creation_date;
}
我遇到的编译器错误有所不同,关于对collect
的方法调用不适用于自变量。
您的Map<Long, Double>
返回类型是正确的,但是出了问题的是您的ToIntFunction<? super T>
。当使该方法通用时,就是说调用方可以控制T
;它可以控制调用方C0的权限。调用者可以提供类型参数,例如: