简体   繁体   中英

Java: polymorphism applied to Map generic types

I want to have a function which (for example) outputs all the values of a 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 the following doesn't seem to work:

public void output(Map<String, Object> map, String key) {
    System.out.println(map.get(key).toString()); 
}

Are not both String and Integer of type Object ?

Map<String, String> does not extends Map<String, Object> , just like List<String> does not extend List<Object> . You can set the value type to the ? wildcard:

public void output(Map<String, ?> map, String key) {  // map where the value is of any type
    // we can call toString because value is definitely an Object
    System.out.println(map.get(key).toString());
}

The thing you are looking for is the attempt in Java to introduce polymorphism on Collections and is called Generics . More specific to your use case, Wildcards will fit the bill.

An Unbounded Wildcard (see details in Wildcards link) is used by @manouti in his answer, but you can use something more specific than just that: an Upper Bounded Wildcard .

Eg Map<String, ? extends Object> Map<String, ? extends Object> where Object usually is the most specific but still common class from which all used classes must be derived. For example, if the values in all of your Maps will have a common parent (or 'super') class YourParentClass , then you can replace Object in my example by that class name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM