简体   繁体   English

带有多个键和值的哈希图?

[英]Hashmap with multiple keys and values?

I want to create a HashMap that looks like this: 我想创建一个如下所示的HashMap:

{LOCATION =[China,Sydney, New York,...], NAME = [Bob Smith, Martha Stewart, Amanda Holmes,....], ORGANIZATION = [Matrix Inc, Paragon Pharmaceuticals, Wills Corp.,...]}

I have more than 1 key with multiple values. 我有多个具有多个值的键。 Whats the best way to do this? 最好的方法是什么?

What you need is a Map<String, List<String>> 您需要的是Map<String, List<String>>

example: 例:

Map<String, List<String>> myMap = new HashMap<>();

myMap.put("Location", Arrays.asList("a", "b", "c"));
myMap.put("Name", Arrays.asList("b", "mar", "ama"));
myMap.put("Org", Arrays.asList("ma", "par", "wil"));

System.out.println(myMap);

output: 输出:

{Org=[ma, par, wil], Location=[a, b, c], Name=[b, mar, ama]} {Org = [ma,par,wil],Location = [a,b,c],Name = [b,mar,ama]}

You can create this structure: 您可以创建以下结构:

Map<String, List<String>> map = new HashMap<>();

Although, it'd be more efficient if you know the structure to create an object with the inner lists. 虽然,如果您知道使用内部列表创建对象的结构,效率会更高。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM