0

我已经在下面制作了哈希表,但现在我正在尝试使用相同的键存储多个值,但我无法做到这一点,请告知如何实现这一点,然后如何迭代 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");

各位请指教..!!

4

4 回答 4

0

您可以使用guava mutlimap(或)将您的值更改为 List 实现之一,而不是String使用 HashMap 而不是 HashTable。

例子:

Map<String, List<String>> = new HashMap<String, List<String>>();
于 2013-01-02T16:45:13.743 回答
0

您可以使用列表地图...

Map<String, List<String>> companies = new Hashtable<String, List<String>>();
companies.put("Google", Arrays.asList("United States","France")); //etc...
于 2013-01-02T16:45:49.640 回答
0

不要使用哈希表,使用哈希图,您可以将结构更改为:

Map<String, List<String>>
于 2013-01-02T16:45:57.343 回答
0
  • 不要使用Hashtable,使用HashMap.
  • 如果你使用 Guava(你应该,真的 ;)),使用Multimap.
  • 如果不这样做,请使用HashTable<String, List<String>>; 但是,您将需要一个包装器类,以便List在添加到该列表之前创建密钥。
于 2013-01-02T16:46:14.807 回答