ダイヤモンド演算子を使用して2番目のマップ宣言が最初のものがコンパイルされないのはなぜかと思います。コンパイルエラー:ダイヤモンド演算子を使用した二重ブレースの初期化(匿名の内部クラス)
error: cannot infer type arguments for HashMap; Map map2 = new HashMap<>() { reason: cannot use '<>' with anonymous inner classes where K,V are type-variables: K extends Object declared in class HashMap V extends Object declared in class HashMap
コード:あなたの答えのための
Map<String, String> map1 = new HashMap<String, String>() { //compiles fine
{
put("abc", "abc");
}
};
Map<String, String> map2 = new HashMap<>() { //does not compile
{
put("abc", "abc");
}
};
EDIT
おかげで - 私はより良いコンパイルエラーを読んでいる必要があります。 私はあなたがここに静的初期化子(静的が完全に不足しているキーワードを)持っていないJLS
It is a compile-time error if a class instance creation expression declares an anonymous class using the "<>" form for the class's type arguments.
タイトルには* static *初期化子がありますか? –
@JonSkeetあなたは正しいです、 "二重ブレース"と呼ばれる方法を覚えていません... – assylias
エラーは 'EnumMap'を参照していますが、あなたのコードには' HashMap'しかありません。 –