I have an object defined as follow:
protected Map<String, ? extends List<? extends LightGeometry>> geoms=new HashMap<String,List<LightGeometry>>();
I try to insert in an object that looks conform to the wildcard
ArrayList<LightGeometry> points=new ArrayList<LightGeometry>();
points.add((LightGeometry)new LightPoint(pt));
geoms.put("point", points);
The compiler throws an error that says:
The method
put(String, capture#18-of ? extends List<? extends LightGeometry>) in the typeMap<String,capture#18-of ? extends List<? extends LightGeometry>>is not applicable for the arguments(String, ArrayList<LightGeometry>)
What am I missing?
EDIT: Why I am using the wildcard with the generic types
It basically comes down to being able to assign the list (which I get through a service) to a the geoms object which is in another class, without having to sift through the list to cast.
public void onSuccess(Map<String, ArrayList<LightPolygon>> result) {
// TODO Auto-generated method stub
GWT.log("" + result.size());
Log.debug("" + result.size());
long startTime = System.currentTimeMillis();
if (overlay != null) {
overlay.setData(result);
overlay.update();
Log.debug("time to add features: "
+ (System.currentTimeMillis() - startTime));
}
}
If I were to make the geoms variable a Map<String, List<LightGeometry>> then I get a cast error that says that I can’t assign a Map<String,ArrayList<LightPolygon>> to a ‘Map>`
A generic wildcard does not mean “Anything that extends List” It means “Some specific thing that extends List, but I don’t know what that specific thing is.”
Therefore it is illegal to add anything to the collection, because you don’t know what the specific thing is! It could be a map of LinkedLists, and here you are putting an ArrayList in it, which would cause someone else to get class cast exception when they assume it’s a LinkedList.
If you want a Map that holds any type of list, just declare it as a Map of Lists, not a Map of ? extends List.
—
tl;dr – it does what you want without the wildcards. Wildcards are usually used for specifying method signatures where it doesn’t matter if you know what the types are. If you just want a collection that holds subclasses, you just specify the supertype.
example:
No casting!