3

如果我有两个哈希图,类型

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

在警察和救护车都扩展救援的地方,我怎么能有这样的方法

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

既不是这个,也不是将返回类型更改为

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

似乎工作。

多谢。

4

2 回答 2

3

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.

However, using ? 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
于 2009-08-27T13:51:51.173 回答
0

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>>();
于 2009-08-27T13:46:30.463 回答