如果我有集合Point,如何在单次迭代中使用Java 8流计算x,y的平均值.
下面的示例在输入集合上创建两个流和迭代两次,以计算x和y的平均值.他们用计算机平均x,y在使用java 8 lambda的单次迭代中的任何方式:
List<Point2D.Float> points =
Arrays.asList(new Point2D.Float(10.0f,11.0f), new Point2D.Float(1.0f,2.9f));
// java 8, iterates twice
double xAvg = points.stream().mapToDouble( p -> p.x).average().getAsDouble();
double yAvg = points.stream().mapToDouble( p -> p.y).average().getAsDouble();
Run Code Online (Sandbox Code Playgroud) 我必须生成用于导出的折线图(另存为 png)而不显示图表。我使用了 JavaFX 网站上的现有示例。这是正确的做法吗?
这是用于生成 png 图像的示例程序,
public class FxChartDemo extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
stage.setTitle("Line Chart Sample");
//defining the axes
final NumberAxis xAxis = new NumberAxis();
final NumberAxis yAxis = new NumberAxis();
xAxis.setLabel("Number of Month");
xAxis.setLabel("Number of Month");
//creating the chart
LineChart<Number, Number> lineChart =
new LineChart<Number, Number>(xAxis, yAxis);
lineChart.setTitle("Stock Monitoring, 2010");
//defining a series
XYChart.Series series = new XYChart.Series();
series.setName("My portfolio");
//populating the series …Run Code Online (Sandbox Code Playgroud)