I have a HashMap where the values are ArrayLists, and I'm trying to write a function to accept shared instances of these HashMaps
HashMap<String, ArrayList<Integer>> myMap = new HashMap<String, ArrayList<Integer>>(); public static void foo(HashMap<?, ArrayList<?>> a) {} public static void bar(HashMap<?, ? extends ArrayList<?>> a) {}
Error message
The foo(HashMap<?,ArrayList<?>>) method foo(HashMap<?,ArrayList<?>>) in the Example type is not applicable for arguments ( HashMap<String,ArrayList<Integer>> ).
Why do I need to have a wildcard for ArrayList extensions?
I thought that having an ArrayList<?> (Without ? extends ), I could restrict the function to only HashMaps using ArrayList values.
I also know that the following general method works:
public static <K,V> void printer(HashMap<K, ArrayList<V>> list) { }
Which behaves the way I thought ArrayList<?> Will work. Can someone explain the subtleties here?
source share