编译器在这里给了我一个非静态方法错误,我已经知道这并不意味着它一定是问题所在,但我真的找不到其他任何东西,特别是因为我在不同的类中有相同的方法,只是为了传递播放一切工作。
public Map<Integer, Map<Integer, Double>> setup(ArrayList<RunPlay> play){
Map<Integer, Map<Integer,Double>> map =
plays.stream()
.collect(
Collectors.groupingBy(RunPlay::getYardline, Collectors.groupingBy(RunPlay::getDown, Collectors.averagingDouble(PassPlay::getPoints)))
);
return map;
下面是RunPlay类:
public class RunPlay {
private int yardline;
private int down;
private int togo;
private int gained;
private int td;
public RunPlay(int yardline, int down, int togo, int gained, int td){
this.down=down;
this.gained=gained;
this.td=td;
this.togo=togo;
this.yardline=yardline;
}
public double getPoints(){
double result=0;
result+=((getGained()*0.1)+(td*6));
return result;
}
public int getYardline() {
return yardline;
}
public int getGained() { return gained; }
public int getDown() { return down; }
public int getTd() {
return td;
}
public int getTogo() {
return togo;
}
}
发布于 2018-10-13 10:58:57
stream
管道的元素是RunPlay
实例。因此,当您调用RunPlay::getYardline
时,会在传入的对象上调用相关方法,在本例中,该对象是一个RunPlay
实例。但是如何调用PassPlay::getPoints
,这在使用方法引用的上下文中是不可能的。因此,如果您需要这样做,您必须使用lambda表达式,假设该方法是一个实例方法,
Map<Integer, Map<Integer, Double>> map = plays.stream()
.collect(Collectors.groupingBy(RunPlay::getYardline, Collectors.groupingBy(RunPlay::getDown,
Collectors.averagingDouble(ignored -> new PassPlay().getPoints()))));
但是,您可以使用上面在此上下文中使用的相同方法引用,这是合法的。
Function<PassPlay, Double> toDoubleFn = PassPlay::getPoints;
因此,将在传入的实例上调用getPoints
方法。
https://stackoverflow.com/questions/52786455
复制相似问题