JavaFX条形图元素上的Java 8流错误
在JavaFXBarChart
的getData()
上使用流时,我收到以下错误
error: invalid method reference
.map(XYChart.Series::getName);
^ non-static method getName() cannot be referenced from a static context
error: incompatible types: invalid method reference
.map(XYChart.Series::getName);
^ method getName in class Series<X,Y> cannot be applied to given types
required: no arguments
found: Object
reason: actual and formal argument lists differ in length where X,Y are type-variables:
X extends Object declared in class Series
Y extends Object declared in class Series
public class MainController{
public BarChart barChart; // from fxml file
}
以下代码将成功打印条形图的类名
List<XYChart.Series> test = barChart.getData();
List<String> testList = test.stream().map(XYChart.Series::getName).collect(Collectors.toList());
System.out.println(testList);
然而,这段代码在编译时会失败
List<String> testList = barChart.getData().stream()
.map(XYChart.Series::getName) // error line
.collect(Collectors.toList());
System.out.println(testList);
为什么会抛出此错误,为什么只在第一个示例中抛出,而在后一个示例中不抛出?这两个代码示例之间的区别是什么
[使用Gradle和Java版本1.8.0_144运行]
共 (0) 个答案