How can I get a set of values from a value in a map?

本秂侑毒 提交于 2019-12-12 02:52:55

问题


I have a map that contains values which could exist for multiple keys. So I figured I needed to setup a map like this: Map<Integer, Set<Long>> myMap = new HashMap<Integer, Set<Long>>

I would like to retrieve a value from a key's set of values.

The problem is that I don't know how to add a long value to the set in myMap and I don't know how to check if myMap contains a value in it's values' set.

Is this implementation incorrect?


回答1:


You need something like this.

if(myMap.containsKey(myKey)){
    myMap.get(myKey).add(value);
}


来源:https://stackoverflow.com/questions/18131409/how-can-i-get-a-set-of-values-from-a-value-in-a-map

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!