Java: polymorphism applied to Map generic types -
i want have function (for example) outputs values of map in both cases:
map<string, string> map1 = new hashmap<string, string>(); map<string, integer> map2 = new hashmap<string, integer>(); output(map1, "1234"); output(map2, "4321");
and following doesn't seem work:
public void output(map<string, object> map, string key) { system.out.println(map.get(key).tostring()); }
are not both string
, integer
of type object
?
map<string, string>
not extends map<string, object>
, list<string>
not extend list<object>
. can set value type ?
wildcard:
public void output(map<string, ?> map, string key) { // map value of type // can call tostring because value object system.out.println(map.get(key).tostring()); }
Comments
Post a Comment