Can somebody explain me why this
Map<String, List<String>> foo = new HashMap<String, LinkedList<String>>();
generates a type mismatch error ?
Type mismatch: cannot convert from HashMap< String,LinkedList< String>> to Iterators.Map< String,List< String>>
Hashmap implements the Map interface, and LinkedList implements the List interface. Moreover, this
List<String> foo = new LinkedList<String>();
works...
Thanks
Because a Map<String, List<String>>
allows you to put
an ArrayList<String>
into it, but doing so would violate the type integrity of a HashMap<String, LinkedList<String>>
.
Either declare your HashMap
as a HashMap<String, List<String>>
or your variable as a Map<String, LinkedList<String>>
or Map<String, ? extends List<String>>
.
The more immediate problem is that you have imported the wrong Map
class (something called Iterators.Map
) or you have another class (or inner class rather) called Map
in the same package as this code. You want to import java.util.Map
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With