简体   繁体   中英

java generics/inheritance in nested hashmap

if i have two hashmaps, of types

HashMap<Integer, HashMap<Integer, Police>> time_id_police;  
HashMap<Integer, HashMap<Integer, Ambulance>> time_id_ambulance;

where Police and Ambulance both extend Rescue, how can i have a method like

HashMap<Integer, HashMap<Integer, Rescue>> getRescue(){
   if (a) return time_id_police;
   else return time_id_ambulance;
}

neither this, nor changing the return type to

HashMap<Integer, HashMap<Integer, ? extends Rescue>> 

seems to work.

thanks a lot.

Clearly HashMap<Integer, HashMap<Integer, Rescue>> is wrong because then a value could be replaced in time_id_police with a HashMap<Integer, Ambulance> . A similar thing could be done if you replaced Rescue with ? extends Rescue ? extends Rescue .

However, using ? extends ? extends twice gives us something that wont break the type system.

HashMap<Integer, ? extends HashMap<Integer, ? extends Rescue>> getRescue() {

Most Java programmers prefer to use the more general Map in types rather than a specific implementation.

Map<Integer, ? extends Map<Integer, ? extends Rescue>> getRescue() {

Incidentally, if you change the body of your method to use the more concise ternary operator:

   return a ? time_id_police : time_id_ambulance;

You get a slightly more helpful error message, as the compiler works out the type for you:

R.java:18: incompatible types
found   : java.util.HashMap<java.lang.Integer,capture of ? extends java.util.HashMap<java.lang.Integer,? extends Rescue>>
required: java.util.HashMap<java.lang.Integer,java.util.HashMap<java.lang.Integer,Rescue>>
   return a ? time_id_police : time_id_ambulance;
        ^
1 error

Change your declarations of time_id_police and time_id_ambulance to

HashMap<Integer, HashMap<Integer, Rescue>> time_id_police;
HashMap<Integer, HashMap<Integer, Rescue>> time_id_ambulance;

you might also want to declare them as Map instead of HashMap, this way if you ever decide to change the Map implementation you use, you'll only have to make a change in one place (where you instanciate your object) rather than in many places (where you use your object)

Map<Integer, Map<Integer, Rescue>> time_id_police = new HashMap<Integer, HashMap<Integer, Rescue>>();

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