私はこの方法を持っています(これは私の元の問題を単純化したものです):
public List<AbstractMap.SimpleEntry<String, List<?>>> method(List<?> list) {
return Collections.singletonList(new AbstractMap.SimpleEntry<>("", list));
}
ただし、これによりコンパイル エラーが発生します。
Console.java:40: error: incompatible types return Collections.singletonList(new AbstractMap.SimpleEntry<>("", list)); ^ required: List<SimpleEntry<String,List<?>>> found: List<SimpleEntry<String,List<CAP#1>>> where CAP#1 is a fresh type-variable: CAP#1 extends Object from capture of ? 10 errors
トップレベルのメソッドで型のインスタンス化を指定しようとすると:
return Collections.<AbstractMap.SimpleEntry<String, List<?>>>singletonList(new AbstractMap.SimpleEntry<>("", list));
別のエラーが発生します。
Console.java:40: error: method singletonList in class Collections cannot be applied to given types; return Collections.<AbstractMap.SimpleEntry<String, List<?>>>singletonList(new AbstractMap.SimpleEntry<>("", list)); ^ required: T found: SimpleEntry<String,List<CAP#1>> reason: actual argument SimpleEntry<String,List<CAP#1>> cannot be converted to SimpleEntry<String,List<?>> by method invocation conversion where T is a type-variable: T extends Object declared in method <T>singletonList(T) where CAP#1 is a fresh type-variable: CAP#1 extends Object from capture of ? 10 errors
内部メソッドで型パラメーターを指定した場合にのみ、すべてが機能します。
return Collections.singletonList(new AbstractMap.SimpleEntry<String, List<?>>("", list));
ここで何が起こっているかを理解しているふりさえしません。これはキャプチャ変換と関係があると思われます(関連する質問here ) が、外側のメソッドでジェネリックを指定しても機能しないのに、内側のメソッドでは機能する理由がわかりません。Java は戻り値の型を使用して、このようなネストされた呼び出しの型引数を推測しませんか? 何が起きてる?