得到一个Map<String, ? extends Map<String, Integer>> mapOfMaps变量。
Map<String, Integer> result = mapOfMaps.get("aaa");
Run Code Online (Sandbox Code Playgroud)
有效,但是
Map<String, Integer> result = mapOfMaps.getOrDefault("aaa",Collections.emptyMap());
Run Code Online (Sandbox Code Playgroud)
说
类型 Map<String,capture#1-of ? 中的方法 getOrDefault(Object, capture#1-of ? extends Map<String,Integer>) extends Map<String,Integer>> 不适用于参数 (String, Map<String,Integer>)
同样适用于
Map<String, Integer> result = mapOfMaps.getOrDefault("aaa",Collections.<String,Integer>emptyMap());
Run Code Online (Sandbox Code Playgroud)
或者
Map<String, Integer> result = mapOfMaps.getOrDefault("aaa",(Map<String,Integer>)Collections.EMPTY_MAP);
Run Code Online (Sandbox Code Playgroud)
甚至
Map<String, Integer> result = mapOfMaps.getOrDefault("aaa",new HashMap<String, Integer>());
Run Code Online (Sandbox Code Playgroud)
有没有办法像这样使用 getOrDefault 或者我必须使用笨重的方式?
Map<String, Integer> result = mapOfMaps.get("aaa");
if( result == null ) {
result = Collections.emptyMap();
}
Run Code Online (Sandbox Code Playgroud)