This can be done by reflection for flexibility.
import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.DoubleStream; public class DoubleReflection { public static void main(String[] args) throws Exception { List<Point> points = new ArrayList<Point>(); points.add(new Point(10, 11)); points.add(new Point(12, 13)); points.add(new Point(14, 15)); double[] array = points.stream().flatMapToDouble((row) -> { Field[] fields = row.getClass().getDeclaredFields(); return Arrays.stream(fields).flatMapToDouble(field -> { try { field.setAccessible(true); return DoubleStream.of(field.getDouble(row)); } catch (IllegalArgumentException | IllegalAccessException e) { e.printStackTrace(); return null; } }); }).toArray(); System.out.println(Arrays.toString(array)); } } class Point { public double a; private double b; public Point(double a, double b) { this.a = a; this.b = b; } public double getB() { return b; } }
Output: [10.0, 11.0, 12.0, 13.0, 14.0, 15.0]
source share