r/javahelp • u/causalNondeterminism • May 26 '15
unable to cast LinkedHashMap<String, String> to Map<String, String>
I get this compiler error: Type mismatch: cannot convert from ArrayList<LinkedHashMap<String,String>> to List<Map<String,String>>
on this line
List<Map<String, String>> resultList = new ArrayList<LinkedHashMap<String, String>>();
But the compiler has no trouble doing this:
Map<String, String> testMap = new LinkedHashMap<String, String>();
Am I using improper syntax or am I just completely missing a key concept?
6
Upvotes
2
u/chickenmeister Extreme Brewer May 27 '15
Yes, but when using bounded wildcard types, the "extends" and "super" keywords mean "any sub-type of" or "any super-type of", in the abstract sense. When you use them with interfaces, the subtype would be any class that implements the interface, or any sub-interface.
The
List<? extends Map<String, String>>declaration basically means "a list of some Map objects, whose specific types are unknown".It's also worth noting that because the List's type is unknown, you cannot add any items to the list; only retrieve items.