1

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 type Map<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>`

Shashi
  • 12,487
  • 17
  • 65
  • 111
giscope
  • 77
  • 1
  • 8

1 Answers1

7

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:

Map<String, List<Number>> map = new HashMap<String,List<Number>>();

List<Number> integers =new ArrayList<Number>();
integers.add(Integer.valueOf(1));
map.put("integers", integers);

List<Number> floats = new LinkedList<Number>(); 
floats.add(Float.valueOf(5.2f));
map.put("floats", floats);

No casting!

Affe
  • 47,174
  • 11
  • 83
  • 83
  • 1
    Java generics are not covariant, it is inherently un-typesafe. The compiler forcing you to make the cast as its way of making you say "I am aware this collection is no longer guaranteed typesafe by the compiler." If you don't want to do the casts, you have to declare your lists as lists of the denominator. – Affe Apr 26 '12 at 22:11