extends, put. , , . , <String, Object>, , .
, , @SuppressWarnings("unchecked") , .
extends vs super, .
, Object, .
Map<String, Number> strToNum = new HashMap<String, Number>();
strToNum.put("one", Integer.valueOf(1));
Map<String, String> strToStr = new HashMap<String, String>();
strToStr.put("one", "1");
Map<String, ? extends Object> strToUnk = randomBoolean() ? strToNum : strToStr;
strToUnk.put("null", null);
strToUnk.put("two", Integer.valueOf(2));
strToUnk.put("two", "2");
, put extends.
, get:
Object value = strToUnk.get("one");
, "put" "get", "super" , :
Map<String, Number> strToNum = new HashMap<String, Number>();
Map<String, Object> strToObj = new HashMap<String, Object>();
Map<String, ? super Number> strToNumBase;
if (randomBoolean()) {
strToNumBase = strToNum;
} else {
strToNumBase = strToObj;
}
strToNumBase.put("two", Double.valueOf(2.0d));
Number n = strToNumBase.get("one");