-1

I have lists:

List<Point[]> listPoints = ...
List<Quadrangle> quadrangles = ...

How convert List<Point[]> to List<Quadrangle> with Stream in Java 8?

Need replace:

for(Point[] groupPoints : listPoints) {
            quadrangles.add(new Quadrangle(groupPoints));
}

Object Quadrangle has the constructor:

public Quadrangle(Point[] points) {
        this.p1 = points[0];
        this.p2 = points[1];
        this.p3 = points[2];
        this.p4 = points[3];
    }

I tried this:

List<Quadrangle> quadrangles = listPoints.stream()
                .collect(Collectors.toCollection(o -> new Quadrangle(o)));

but this doesn't work for me. I get error:

Required  type:Point[]
Provided: <lambda parameter>

I thought that the lambda parameter o is the array Point [], but isn't it.

0

1 Answer 1

2

Try it like this.

  • Stream the list of point arrays.
  • Map to a new instance of Quadrangle (this applies the constructor argument)
  • return a list of Quadrangle.
List<Quadrangle> quads = listPoints.stream()
.map(Quadrangle::new)
.collect(Collectors.toList());
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.