英文:
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<String, Map<String, List>> 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("Distinct", "adjective", "Familiar. Worked in Java.")
Another example is:
a3("Distinct", "adjective", "Unique. No duplicates. Clearly different or of a different kind.")
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<String, String> myMap = new HashMap<String, String>();
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 <-> 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 `"Distinct", "adjective", "Familiar. Worked in Java."` 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'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>
专注分享java语言的经验与见解,让所有开发者获益!
评论