Java 将值放入内部映射

Java 将值放入内部映射,java,spring,collections,Java,Spring,Collections,我有一个ConcurrentHashMap,如下所示: private Map<String,Map<String,Set<PublicKey>>> instancePairs = new ConcurrentHashMap<>(); Intellij Idea给了我这个错误: 正如“DDovzhenko”所提到的,您需要按照以下思路做一些事情 //Get the map containing the ID as keys, if it does

我有一个ConcurrentHashMap,如下所示:

private Map<String,Map<String,Set<PublicKey>>> instancePairs = new ConcurrentHashMap<>();
Intellij Idea给了我这个错误:

正如“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);
//获取包含ID作为键的映射,如果它不存在,则创建一个。
Map mapValuesForName=instancePairs.getOrDefault(inMemoryInstance.getUsername(),new ConcurrentHashMap());
//根据Id放置publicKeySet。
mapValuesForName.putIfAbsent(在MemoryInstance.getId()中,publicKeySet);
//将可能更改的/新的值存储回原始映射中。
instancePairs.put(mapValuesForName);

您应该首先按键获取内部映射,如果不存在则创建它,然后才将值放入内部映射。
putIfAbsent()
需要两个参数,但您要给它三个参数。
//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);