我有一個看起來像這樣的 ConcurrentHashMap:private Map<String,Map<String,Set<PublicKey>>> instancePairs = new ConcurrentHashMap<>();以及一個應該填充此哈希圖的方法。但我無法弄清楚如何將值放入地圖中目前我有:instancePairs.putIfAbsent(inMemoryInstance.getUsername(), inMemoryInstance.getId() , publicKeySet);Intellij Idea 給我這個錯誤:
1 回答

鳳凰求蠱
TA貢獻1825條經(jīng)驗 獲得超4個贊
正如“DDovzhenko”所提到的,你需要按照以下幾行做一些事情。
//Get the map containing the ID as keys, if it doesn't exist, then create one.
Map mapValuesForName = instancePairs.getOrDefault(inMemoryInstance.getUsername(), new ConcurrentHashMap<String,Set<PublicKey>>());
//Put the publicKeySet based on the Id.
mapValuesForName.putIfAbsent(inMemoryInstance.getId(), publicKeySet);
//Store the potentially changed/new value back in original map.
instancePairs.put(mapValuesForName);
添加回答
舉報
0/150
提交
取消