将值放入内部Map

时间:2019-05-31 13:27:26

标签: java spring collections

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

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

还有一种应该填充此哈希图的方法。

但是我不知道如何将值放入地图中

当前我有:

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

Intellij Idea给我这个错误:

picture

1 个答案:

答案 0 :(得分:0)

正如“ 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);