The reason is that the ?
in List<?>
could be "anything", but a different "anything" in each Map
entry. That is, it would accept a List<String>
in one entry, and a List<Integer>
in another.
But you are passing in a Map
that has the same type of List
in every entry, so the type is not bound in the same way or the to same degree for freedom.
The "fix" is to lock the type to a specific type, but still being "anything" - just the same "anything* in every entry, by typing the method:
public static <T> void accept(Map<String, List<T>> multiMap) // complies
or if your method really doesn't need to know which type, use a wildcard to wrap the type:
public static void accept(Map<String, ? extends List<?>> multiMap) // compiles
This last version works because the type of the list, although being a wildcard, is fixed to an unknown, but consistent, type when called.
I find the typed version easier to read (and code), and the type is there for use should you decide later that your method needs to know the type.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…