私が持っていて、int[] array = {1, 2, 3}
以下の値でハッシュマップを初期化したい場合、それを行うためのより良い方法はありますか?
Map<Integer,Boolean> map = new HashMap<Integer,Boolean>();
map.put(1,false);
map.put(2,false);
map.put(3,false);
私が持っていて、int[] array = {1, 2, 3}
以下の値でハッシュマップを初期化したい場合、それを行うためのより良い方法はありますか?
Map<Integer,Boolean> map = new HashMap<Integer,Boolean>();
map.put(1,false);
map.put(2,false);
map.put(3,false);
for (int i: array) {
map.put(i, false);
}
グアバを使用する場合は、
ImmutableMap.of(1, false, 2, false, 3, false);
また、
ImmutableMap.builder().put(1, false).put(2, false).put(3, false).build()
初期化する別の方法は次のとおりです。
Map<Integer,Boolean> map = new HashMap<Integer, Boolean>() {
{
put(1,false);
put(2,false);
put(3,false);
}