问题
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