3

I have this sample code showing different method signatures:

public class Main {
    public static interface MyMap extends Map {}
    public void func1(List<MyMap> m) {}
    public void func2(List<Map> m) {}
    public void func3(List<? extends Map> m) {}
    public void func4(List<? extends Map<?, ?>> m) {}
    public <M extends Map<?, ?>> void func5(List<M> m) {}
    public static void main(String[] args) {
        List<MyMap> myMap = null;
        Main main = new Main();
        main.func1(myMap); // OK
        main.func2(myMap); // not applicable
        main.func3(myMap); // OK
        main.func4(myMap); // not applicable
        main.func5(myMap); // OK
    }
}

When I have MyMap extending the raw type Map I have these confusions about method signature compatibility in Java.

As seen func1 is a control test, obviously IDE will not complain. In func2, I can understand MyMap is not exactly a Map, hence IDE is complaining. After updating the signature like func3 it works again. As for func4, I sort of thinking Map<?,?> is not the same as a raw type Map, thats why IDE is complaining again? But then what puzzles me most is func5 because it looks equivalent to func4 but IDE is not complaining?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
user1589188
  • 5,316
  • 17
  • 67
  • 130

1 Answers1

0

I think this was answered already:

Difference between Bounded Type parameter and Upper Bound Wildcard

Only exception, you added Raw Type Eresure, but I don't think it somehow influence the result.

Also, I thinks you may find next useful:

main.func4((List<? extends Map<?, ?>>) myMap); // OK, but unchecked warning
Community
  • 1
  • 1
ar4ers
  • 740
  • 5
  • 19