我有以下结构
public class Point {
private final double x;
private final double y;
// imagine required args constructor and getter for both fields
}
现在,我已经定义了这些要点。
List<Point> points = new ArrayList<>();
points.add(new Point(0,0));
points.add(new Point(0,1));
points.add(new Point(0,2));
points.add(new Point(0,3));
数据根本无关紧要,只是点列表(上面只是一个简单而快速的示例)。
如何以Java 8方式将此列表转换为双精度数组(double []数组)?
这应该做。
points.stream()
.flatMapToDouble(point -> DoubleStream.of(point.getX(), point.getY()))
.toArray();
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句