如何将键值对从一个映射映射到另一个映射
如果我有一个包含以下数据的映射:
"a", "hello"
"b", "bye"
"c", "good morning"
以及另一个包含以下数据的映射:
"key1","a"
"key2", "b"
"key3", "c"
是否可以执行操作,以便我可以将第二个映射的值映射到作为我的第一个映射的键上?这将导致最终的地图如下所示:
"key1","hello"
"key2", "bye"
"key3", "good morning"
If I have a map with the following data:
"a", "hello"
"b", "bye"
"c", "good morning"
and a second map with the following data:
"key1","a"
"key2", "b"
"key3", "c"
is it then possible to perform an operation such that I can map the value of my second map onto the key as my first map? Which would result in the final map looking like this:
"key1","hello"
"key2", "bye"
"key3", "good morning"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
显然,您想要由第二个映射中的键组成的第三个映射,以及第一个映射中的匹配值。
Apparently you want a third map made of keys from the second map, and matching values from the first map.
对于您想要做的事情,下面的代码应该可以工作
The below code should work, for what you are trying to do
这样:
In this way:
这可能对你有用。请注意,这将创建第三张地图。
map2
的条目集条目的键
作为结果映射的键条目的值
作为检索以下值的键map1
打印
This may work for you. Note that this creates a third map.
map2
entry's key
as the key to the result mapentry's value
as the key to retrieve the value ofmap1
prints
正如我在评论中提到的,下面的语句将打印
"hello"
。如果您想在同一代码中访问这两个地图的内容,则无需创建第三个地图。使用以下代码,您可以迭代打印第二个映射的键和第一个映射的值(假设所有值第二张地图的属性作为键存在于第一张地图中)。
仅当您需要将数据从这两个映射传递到应用程序的另一部分时,创建第三个组合映射才可能有意义。可以使用 Stream IPA 来完成,如下所示:
As I've mentioned in the comment the statement below will print
"hello"
. And there's no need to do create a third map if you want to access the content of these two maps right in the same code.With the following code, you can iterate print the keys of the second map and the values of the first map (with the assumption that all values of the second map are present in the first map as keys).
And only if you need to pass the data from these two maps to another part of your application it might make sense to create the third combined map. It could be done with the Stream IPA like that: