How can I get a set of values from a value in a ma

2019-09-06 18:44发布

问题:

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);
}


标签: java map set