0

I am trying to create a multimap as follows:

Multimap<String, ? extends A> order = LinkedListMultimap.create();
B b = new B();
order.put("key", b); // shows error

where B is

class B extends A {} 

The error is as follows:

The method put(String, capture#2-of ? extends A) in the type Multimap<String,capture#2-of ? extends A> is not applicable for the arguments (String, B).

I should be able to add a B since B extends A. But I am not able to.

Paul Bellora
  • 54,340
  • 18
  • 130
  • 181
Ikshvak
  • 205
  • 2
  • 8
  • 17

2 Answers2

2

If you mean a multimap from String to objects of type A or any subtype of A, then that's a Multimap<String, A>. If you mean a multimap from String to objects of some specific but unknown subtype of A, then that's a Multimap<String, ? extends A>.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
  • so for example if C also extends A and I want to add both B as well as C to the multimap. In order to get that should I not configure the Multimap as ? extends A?? – Ikshvak Apr 25 '13 at 19:42
  • 1
    No, you shouldn't; in that case you _really do_ want `Multimap`. – Louis Wasserman Apr 25 '13 at 19:45
  • 1
    A `Multimap` means that there is some specific type `X` that extends `A`, and the `Multimap` is actually a `Multimap`, but you don't know what `X` is. You can't put _any_ value except `null` into a `Multimap`. – Louis Wasserman Apr 25 '13 at 19:52
0

You can not add values to order, because they key is

? extends A

and compiler doesn't know the lower bound type.

user123454321
  • 1,028
  • 8
  • 26