我已经在下面创建了哈希表但是现在我尝试使用相同的密钥存储多个值,但我无法做到这一点请告知如何实现该目标,然后如何迭代overit以显示附加的多个值键..
Hashtable companies = new Hashtable();
// Java Hashtable example to put object into Hashtable
// put(key, value) is used to insert object into map
companies.put("Google", "United States");
companies.put("Nokia", "Finland");
companies.put("Sony", "Japan");
我想实现像......
Hashtable companies = new Hashtable();
// Java Hashtable example to put object into Hashtable
// put(key, value) is used to insert object into map
companies.put("Google", "United States","France");
companies.put("Nokia", "Finland","Japan");
companies.put("Sony", "Japan", "indonesia");
人们请指教.. !!
答案 0 :(得分:0)
您可以使用guava mutlimap(或)将值更改为List实现之一而不是String
,并且更好地使用HashMap而不是HashTable。
示例:
Map<String, List<String>> = new HashMap<String, List<String>>();
答案 1 :(得分:0)
您可以使用列表地图......
Map<String, List<String>> companies = new Hashtable<String, List<String>>();
companies.put("Google", Arrays.asList("United States","France")); //etc...
答案 2 :(得分:0)
不要使用哈希表,使用hashmap,你只需将结构更改为:
Map<String, List<String>>
答案 3 :(得分:0)
Hashtable
,请使用HashMap
。Multimap
。HashTable<String, List<String>>
;您需要一个包装器类,以便在添加到该列表之前在密钥中创建List
。