如何向Map中的Set添加值? [英] How do I add values to a Set inside a Map?
本文介绍了如何向Map中的Set添加值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个地图 Map< String,Set< Integer>> myMap;
,现在我需要与它进行交互,我该怎么做?
I have this map Map<String, Set<Integer>> myMap;
, now I need to interact with it, how do I do it?
键是:苹果,橙色,葡萄等。 br>
每个集合将包含随机数: 1-9
Keys are: "apple", "orange", "grape", etc.
Each set will contain random numbers: 1-9
我需要创建一个Map(HashMap或TreeMap),有字符串用于键值和值的值。我需要返回给定一个键。我还需要能够根据一个键填充多个数字的每个集合。不知道如何解决这个问题。任何想法?
I need to create a Map (HashMap or TreeMap) that has Strings for keys and sets for the values. I need to return the set given a key. I also need to be able to fill each set with multiple numbers based on a key. Not sure how to approach this problem. Any thoughts?
推荐答案
如果我理解正确,不难。
Not difficult if I understand correctly.
Map<String, Set<Integer>> reqdMap = new HashMap<String, Set<Integer>>();
//Form the set corresponding to apple.
Set<Integer> appleSet = new HashSet<Integer>();
appleSet.add(1);
...
reqdMap.put("apple", appleSet);
//To Retrieve
appleSet = reqdMap.get("apple");
这篇关于如何向Map中的Set添加值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文