I have a map from X to Y, and I'd like to create a deep-copy of the ranges.
So a normal copy can be done by:
public<X,Y> Map<X,Y> duplicateMapToObject(Map<X,Y> map)
{
Map<X,Y> newMap = new HashMap();
for(X x : map.keySet())
{
newMap.put(x,map.get(x));
}
return newMap;
}
And I've tried to make a deep copy of Y (assuming Y has a constructor that can create a new Y-object by using an existing Y object):
public<X,Y> Map<X,Y> duplicateMapToObject(Map<X,Y> map)
{
Map<X,Y> newMap = new HashMap();
for(X x : map.keySet())
{
Y y = map.get(x);
Y newY = new Y(y);
newMap.put(x,newY);
}
return newMap;
}
However, I get an error "Type of parameter 'Y' cannot be instantiated directly". I'm assuming this is because the compiler doesn't know whether Y has a constructor that can create a new Y-object by using an existing Y-object, which is my assumption. However, I'd expect this to be a runtime error, in the case I call this method on a function without the appropriate constructor.
Is there a way to bypass this?