将其翻译为“推断的第二个泛型参数未被缩小”,解决方法为使用显式类型参数来缩小泛型参数范围。例如:
List<String> list = new ArrayList<>();
list.add("hello");
String str = list.get(0); // 不需要使用显式类型参数缩小范围
Map<String, Integer> map = new HashMap<>();
map.put("one", 1);
int num = map.get("one"); // 不需要使用显式类型参数缩小范围
Map<String, String> map = new HashMap<>();
map.put("one", "1");
String str = map.get("one"); // 需要使用显式类型参数缩小范围
这样,就可以避免出现“Inferred second generic argument is not narrowed”这样的警告了。