[Solved] How to convert java list of objects to 2D array?


Not 100% sure what you are asking, but I’ll give it a try. Assuming you have a list of Points…

List<Point2D> points = Arrays.asList(new Point2D.Double(12., 34.),
                                     new Point2D.Double(56., 78.));

… you can turn that list into a 2D-array like this:

double[][] array = points.stream()
        .map(p -> new double[] {p.getX(), p.getY()})
        .toArray(double[][]::new);

… or into a nested list like this:

List<List<Double>> list = points.stream()
        .map(p -> Arrays.asList(p.getX(), p.getY()))
        .collect(Collectors.toList());

In both cases, the result looks like this [[12.0, 34.0], [56.0, 78.0]], as an array or a list, respectively.

solved How to convert java list of objects to 2D array?