一般的なタイプのテストを行いました。HashMapの値の汎用タイプ(リスト<? extends>)
class A {}
class B extends A {}
が2つの方法です:
private static void extendTest(List<? extends A> superList) {
System.out.println("extendTest over.");
}
private static void extendMapTest(HashMap<String, List<? extends A>> superMap) {
System.out.println("extendMapTest over.");
}
は、私はlist
とmap
を作成します。
List<B> childList = new ArrayList<>();
childList.add(new B());
extendTest(childList); // TAG1: it is OK.
HashMap<String, List<B>> childMap = new HashMap<>();
childMap.put("hello", childList);
extendMapTest(childMap); // TAG2: ERROR!
TAG1はOKで、TAG2は誤りである理由? TAG2を修正するにはどうすればよいですか?
エラーを報告する。 – nbrooks