猿问

将值放入内部 Map

我有一个看起来像这样的 ConcurrentHashMap:

private Map<String,Map<String,Set<PublicKey>>> instancePairs = new ConcurrentHashMap<>();

以及一个应该填充此哈希图的方法。

但我无法弄清楚如何将值放入地图中

目前我有:

instancePairs.putIfAbsent(inMemoryInstance.getUsername(), inMemoryInstance.getId() , publicKeySet);

Intellij Idea 给我这个错误:


梵蒂冈之花
浏览 105回答 1
1回答

凤凰求蛊

正如“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);
随时随地看视频慕课网APP

相关分类

Java
我要回答