为什么这个地图语句使用这种语法格式?

huangapple 未分类评论47阅读模式
英文:

Why is this map statement formatted with this syntax?

问题

我在查看一个项目时遇到了这个语句,我正在尝试弄清楚它是如何工作的。

private Map<String, Map<String, List>> bank;

我对使用映射有一些经验,但当我看到这行代码时有点困惑。该项目涉及使用枚举对象。

创建枚举对象的一个示例是:

a2("Distinct", "adjective", "Familiar. Worked in Java.")

另一个示例是:

a3("Distinct", "adjective", "Unique. No duplicates. Clearly different or of a different kind.")

我知道 Map 的工作原理(如果我有错误,请纠正我)。在这种情况下,“Distinct” 这个词被映射到词性(词性被映射到包含相同词汇定义的列表)。

我主要的问题是,为什么这是一个有效的语句,为什么语法与通常创建映射的方式不同,比如:

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

任何帮助都将非常感谢,非常感谢您。

英文:

I came across this statement while looking over a project and I'm trying to figure out how it works.

private Map&lt;String, Map&lt;String, List&gt;&gt; bank;

I have some experience with using maps but when I looked at this line I was a bit confused. The project consists of using enumeration objects.

One example of the enumeration objects created would be:

a2(&quot;Distinct&quot;, &quot;adjective&quot;, &quot;Familiar. Worked in Java.&quot;)

Another example is:

a3(&quot;Distinct&quot;, &quot;adjective&quot;, &quot;Unique. No duplicates. Clearly different or of a different kind.&quot;)

I know how the Map works (please correct me if I am wrong). The word, "Distinct", in this case, is being mapped to part of speech (which is mapped to a list containing the definitions of the same word).

The main question I have is, how is this a valid statement, and why is the syntax different than normally creating a map, like this:

Map&lt;String, String&gt; myMap = new HashMap&lt;String, String&gt;();

Any help would be great, thank you so much.

答案1

得分: 0

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

这只是一个将字符串与字符串列表关联起来的映射,类似于 key <-> value1, value2, value3

要添加一个值,只需使用以下方式:

map.computeIfAbsent("key", o -> new ArrayList<>()).add("value1");
map.computeIfAbsent("key", o -> new ArrayList<>()).add("value2");
map.computeIfAbsent("key", o -> new ArrayList<>()).add("value3");

如果您想关联3个字符串,例如 “Distinct”、“adjective”、“Familiar. Worked in Java.”,考虑使用映射的映射(map of maps):

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

要添加一个值,请使用以下结构:

map.computeIfAbsent("Distinct", k -> new HashMap<>()).computeIfAbsent("adjective", k -> new ArrayList<>()).add("Familiar. Worked in Java.");
map.computeIfAbsent("Distinct", k -> new HashMap<>()).computeIfAbsent("adjective", k -> new ArrayList<>()).add("Unique. No duplicates. Clearly different or of a different kind.");

参见 规范文档Baeldung 的教程


<details>
<summary>英文:</summary>

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

This is just a map which associates a string to a list of strings. Like `key &lt;-&gt; value1, value2, value3`

To add a value there just use

map.computeIfAbsent("key", o->new ArrayList<>()).add("value1");
map.computeIfAbsent("key", o->new ArrayList<>()).add("value2");
map.computeIfAbsent("key", o->new ArrayList<>()).add("value3");


If you want to have 3 strings associated, like `&quot;Distinct&quot;, &quot;adjective&quot;, &quot;Familiar. Worked in Java.&quot;` consider using `map of maps`: 

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

To put a value there, use the following construction:

map.computeIfAbsent("Distinct", k->new HashMap<>()).computeIfAbsent("adjective", k->new ArrayList<>()).add("Familiar. Worked in Java.")
map.computeIfAbsent("Distinct", k->new HashMap<>()).computeIfAbsent("adjective", k->new ArrayList<>()).add("Unique. No duplicates. Clearly different or of a different kind.")


See [spec][1] and [Baeldung&#39;s how-to][2]


  [1]: https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#computeIfAbsent-K-java.util.function.Function-
  [2]: https://www.baeldung.com/java-map-computeifabsent

</details>



huangapple
  • 本文由 发表于 2020年5月5日 10:42:36
  • 转载请务必保留本文链接:https://java.coder-hub.com/61604843.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定