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