访问单个Set作为Map的值

时间:2014-05-03 14:55:18

标签: java collections syntax map

当值为集合时,我需要显示Map的键值对中的值。将显示值的键可以指定为方法的参数。我不确定访问它的语法是什么。当它是一个字符串时我成功了。

任何人都可以提出以下建议:

HashMap<String, Set<Integer>> map = new HashMap<>();
Set<Integer> theValues = new Set<>();

public void mapValue(String aString)
{
    if(map.containsKey(aString))
    { 

      System.out.println ("The value of the set is " + theValues  ); 
      // what can go here to show the elements of the set that 
      // is the value map given the key?
    }


}

1 个答案:

答案 0 :(得分:0)

你可能需要这样的东西来迭代每个Map键的Set值:

HashMap<String, Set<Integer>> map = new HashMap<String,Set<Integer>>();
Set<Integer> theValues = new HashSet<Integer>();

public void mapValue(String aString) {
    if(map.containsKey(aString)) {
        System.out.println ("The values of the set is:");
        Set<Integer> set = map.get(aString);
        for (Integer i: set)
            System.out.println(i);
    }
}
相关问题