如何将键值对从一个映射映射到另一个映射

发布于 2025-01-10 21:17:03 字数 317 浏览 4 评论 0原文

如果我有一个包含以下数据的映射:

"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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

去了角落 2025-01-17 21:17:03

显然,您想要由第二个映射中的键组成的第三个映射,以及第一个映射中的匹配值。

Map< String , String > thirdMap = new HashMap<>() ;
for ( Map.Entry< String , String > entry : secondMap.entrySet() ) {
    thirdMap.put(
        entry.getKey() ,                  // Second map’s key.
        firstMap.get( entry.getValue() )  // First map’s value.
    );
}

Apparently you want a third map made of keys from the second map, and matching values from the first map.

Map< String , String > thirdMap = new HashMap<>() ;
for ( Map.Entry< String , String > entry : secondMap.entrySet() ) {
    thirdMap.put(
        entry.getKey() ,                  // Second map’s key.
        firstMap.get( entry.getValue() )  // First map’s value.
    );
}
杯别 2025-01-17 21:17:03

对于您想要做的事情,下面的代码应该可以工作

 public static void main(final String[] args) throws Exception {
    HashMap<String, String> keyToValue = new HashMap<String, String>() {{
        put("a", "hello");
        put("b", "bye");
        put("c", "good morning");
    }};
    HashMap<String, String> keyToSecondaryKey = new HashMap<String, String>() {{
        put("key1", "a");
        put("key2", "b");
        put("key3", "c");
    }};

    keyToSecondaryKey.entrySet().forEach(e-> {
        e.setValue(keyToValue.get(e.getValue()));
    });

    System.out.println(keyToSecondaryKey);
}

The below code should work, for what you are trying to do

 public static void main(final String[] args) throws Exception {
    HashMap<String, String> keyToValue = new HashMap<String, String>() {{
        put("a", "hello");
        put("b", "bye");
        put("c", "good morning");
    }};
    HashMap<String, String> keyToSecondaryKey = new HashMap<String, String>() {{
        put("key1", "a");
        put("key2", "b");
        put("key3", "c");
    }};

    keyToSecondaryKey.entrySet().forEach(e-> {
        e.setValue(keyToValue.get(e.getValue()));
    });

    System.out.println(keyToSecondaryKey);
}
情深已缘浅 2025-01-17 21:17:03

这样:

Map<String, String> map1 = new HashMap<>();
map1.put("a", "hello");
map1.put("b", "bye");
map1.put("c", "good morning");

Map<String, String> map2 = new HashMap<>();
map2.put("key1", "a");
map2.put("key2", "b");
map2.put("key3", "c");

map2.forEach((key2, value2) -> map2.put(key2, map1.get(value2)));

In this way:

Map<String, String> map1 = new HashMap<>();
map1.put("a", "hello");
map1.put("b", "bye");
map1.put("c", "good morning");

Map<String, String> map2 = new HashMap<>();
map2.put("key1", "a");
map2.put("key2", "b");
map2.put("key3", "c");

map2.forEach((key2, value2) -> map2.put(key2, map1.get(value2)));
何处潇湘 2025-01-17 21:17:03

这可能对你有用。请注意,这将创建第三张地图。

Map<String, String> map1 =
        Map.of("a", "hello", "b", "bye", "c", "good morning");

Map<String, String> map2 =
        Map.of("key1", "a", "key2", "b", "key3", "c");
  • 流式传输 map2 的条目集
  • 使用该条目的键作为结果映射的键
  • 使用该条目的值作为检索以下值的键map1
Map<String, String> result = map2.entrySet().stream()
        .collect(Collectors.toMap(Entry::getKey,
                e->map1.get(e.getValue())));
        
result.entrySet().forEach(System.out::println);     

打印

key1=hello
key2=bye
key3=good morning

This may work for you. Note that this creates a third map.

Map<String, String> map1 =
        Map.of("a", "hello", "b", "bye", "c", "good morning");

Map<String, String> map2 =
        Map.of("key1", "a", "key2", "b", "key3", "c");
  • Stream the entry set of map2
  • Use that entry's key as the key to the result map
  • use that entry's value as the key to retrieve the value of map1
Map<String, String> result = map2.entrySet().stream()
        .collect(Collectors.toMap(Entry::getKey,
                e->map1.get(e.getValue())));
        
result.entrySet().forEach(System.out::println);     

prints

key1=hello
key2=bye
key3=good morning
甜柠檬 2025-01-17 21:17:03

正如我在评论中提到的,下面的语句将打印 "hello"。如果您想在同一代码中访问这两个地图的内容,则无需创建第三个地图

System.out.println(fistMap.get(secondMap.get("key1"))); 

使用以下代码,您可以迭代打印第二个映射的和第一个映射的(假设所有值第二张地图的属性作为键存在于第一张地图中)。

fistMap.forEach((k, v) -> System.out.println(k + " -> " + secondMap.get(v)));

仅当您需要将数据从这两个映射传递到应用程序的另一部分时,创建第三个组合映射才可能有意义。可以使用 Stream IPA 来完成,如下所示:

Map<String, String> combinedMap = 
       secondMap.keySet().stream()
            .collect(Collectors.toMap(Function.identity(), // key of the new map
                                      k -> fistMap.get(secondMap.get(k)))); // value of the new map

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.

System.out.println(fistMap.get(secondMap.get("key1"))); 

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).

fistMap.forEach((k, v) -> System.out.println(k + " -> " + secondMap.get(v)));

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:

Map<String, String> combinedMap = 
       secondMap.keySet().stream()
            .collect(Collectors.toMap(Function.identity(), // key of the new map
                                      k -> fistMap.get(secondMap.get(k)))); // value of the new map
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文