2

I have a wrapper class for ConcurrentMap like the following:

public MapWrapper<K, V> implements ConcurrentMap<K, V> {

    private final ConcurrentMap<K, V> wrappedMap;

    ...
    @Override
    public void putAll(Map<? extends K, ? extends V> map) {
        wrappedMap.putAll(map);  // <--- Gives compilation error
    }
    ...
}

The marked line triggers the following compilation error:

method putAll in interface java.util.Map<K,V> cannot be applied to given types;
required: java.util.Map<? extends capture#5 of ? extends K,? extends capture#6 of ?
    extends V>
found: java.util.Map<capture#7 of ? extends K,capture#8 of ? extends V>
reason: actual argument java.util.Map<capture#7 of ? extends K,capture#8 of ? extends V> 
    cannot be converted to java.util.Map<? extends capture#5 of ? extends K,? extends 
    capture#6 of ? extends V> by method invocation conversion

I suspect the unbounded wildcards are the culprit but I can't change the method signature since it is inherited from the ConcurrentMap interface. Any ideas?

Jeff Axelrod
  • 27,676
  • 31
  • 147
  • 246
teto
  • 4,019
  • 4
  • 21
  • 18
  • 4
    What kind of compiler are you using (also, IDE may have it's own, like Eclipse does)? I manage to compile that both within Eclipse, and using javac (1.6); you could also try casting wrappedMap.putAll((Map) m); – Art Licis Jun 04 '11 at 21:20
  • 1
    just tried to reproduce the code you have and... everything works fine. I created a mock `MapWrapper` and instanciated it ``. Then called the `putAll` method with a `HashMap` and everything worked. (using eclipse and Java 1.6) – Yanick Rochon Jun 04 '11 at 21:29
  • Thank you Arthur, I found the error. The thing is that the wrapped map is not really a ConcurrentMap but a class that implements that interface. I was mixing two different things in my code, but since generics were involved the error message really didn't help that much. – teto Jun 04 '11 at 21:51

2 Answers2

0

Have you seen:

What is the difference between bounded wildcard and type parameters?

Community
  • 1
  • 1
Jits
  • 9,647
  • 1
  • 34
  • 27
0

Let's look to signature of putAll

public void putAll(Map<? extends K, ? extends V> m)

... and to error which you got:

cannot be converted to java.util.Map<? extends capture#5 of ? extends K,? extends 

So reason why you can't do it, it's restriction of merging of inheritance tree in Java.

Probably, will be better to write your own implementation of putAll method.

Thanks, hope it will help you.

Orest V
  • 71
  • 3
  • I'm not sure what you mean by "restriction of merging of inheritance tree" but if you read the comments, the OP found that the error was due to an unrelated issue. – Paul Bellora Mar 31 '12 at 21:09