I've been trying to find a way to write a generic function -- possibly not using generics -- to map over a collection.
Let's say I have a function A to B, I'd like to write a function that takes a Collection<A>
and returns a Collection<B>
. NOTE that A and B are not generics, just a way to express a general pattern.
What I have so far is
public static Collection<Point> points2dToPoints(Collection<Point2D> points) {
return points.stream()
.map(Utils::point2dToPoint)
.collect(Collectors.toCollection(() -> points));
}
However, I get a type error in the .collect
, because obviously I want the new collection to be Collection<Point>
but I'm not sure how to get a Supplier for that?
Edit: I would like to be able to use this function in a generic way: if I pass it a Set I get a Set in return, but if I pass it a List I get a list in return. is it even possible to do this?