简体   繁体   English

如何在java中合并两个嵌套的HashMap

[英]How to merge two nested HashMaps in java

I have two nested maps and I try to merge two nested maps and produce a output of two merged HashMap .我有两个嵌套映射,我尝试合并两个嵌套映射并生成两个合并的HashMap的输出。 Below is the code that I use:下面是我使用的代码:

HashMap<String, Object> map = new HashMap<>();

HashMap<String, Object> map1 = new HashMap<>();
map1.put("location", "A");
HashMap<String, Object> map2 = new HashMap<>();
map2.put("geocoordinates", map1);
HashMap<String, Object> map3 = new HashMap<>();
map3.put("TEST", map2);
map.putAll(map3); 

HashMap<String, Object> map11 = new HashMap<>();
map11.put("longitude", "B");
HashMap<String, Object> map12 = new HashMap<>();
map12.put("geocoordinates", map11);
HashMap<String, Object> map13 = new HashMap<>();
map13.put("TEST", map12);

map.putAll(map13); 
System.out.println(map);

The output that I get is:我得到的输出是:

   {TEST={geocoordinates={longitude=B}}}

But I expected both longitude and location key to be nested inside geocoordinates key but only longitude B is there.但我希望longitudelocation键都嵌套在geocoordinates键内,但只有longitude B 存在。 So, How can I get the combined.那么,我怎样才能得到结合。 How achieve this?如何实现这一目标?

Do it as follows:请按以下步骤操作:

import java.util.HashMap;

public class Main {
    public static void main(String[] args) {
        HashMap<String, Object> map = new HashMap<>();

        HashMap<String, Object> map1 = new HashMap<>();
        map1.put("location", "A");
        HashMap<String, Object> map2 = new HashMap<>();
        map2.put("geocoordinates", map1);
        HashMap<String, Object> map3 = new HashMap<>();
        map3.put("TEST1", map2);
        map.putAll(map3);

        HashMap<String, Object> map11 = new HashMap<>();
        map11.put("longitude", "B");
        HashMap<String, Object> map12 = new HashMap<>();
        map12.put("geocoordinates", map11);
        HashMap<String, Object> map13 = new HashMap<>();
        map13.put("TEST2", map12);

        map.putAll(map13);

        System.out.println(map);
    }
}

Output:输出:

{TEST2={geocoordinates={longitude=B}}, TEST1={geocoordinates={location=A}}}

Reason : a Map replaces the old value when you put a new value on the same key (in your case, it is TEST ).原因:当您将新值放在同一个键上时, Map替换旧值(在您的情况下,它是TEST )。 Note that HashMap.putAll() copies all of the mappings from one map into another.请注意HashMap.putAll()所有映射从一个映射复制到另一个映射。 In your code, map.putAll(map3) is equivalent of map.put("TEST",map3) .在您的代码中, map.putAll(map3)相当于map.put("TEST",map3) And, map.putAll(map13) is equivalent of map.put("TEST",map13) which replaces the earlier value, map3 because of the same key, TEST .而且, map.putAll(map13)等价于map.put("TEST",map13)因为相同的键TEST替换了较早的值map3

Update: Adding the following update based on the new requirement mentioned in your comment更新:根据您评论中提到的新要求添加以下更新

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        HashMap<String, Object> map = new HashMap<>();
        List<HashMap> list=new ArrayList<HashMap>();

        HashMap<String, Object> map1 = new HashMap<>();
        map1.put("location", "A");
        list.add(map1);

        HashMap<String, Object> map11 = new HashMap<>();
        map11.put("longitude", "B");
        list.add(map11);

        HashMap<String, Object> map2 = new HashMap<>();
        map2.put("geocoordinates", list);       

        map.put("TEST",map2);

        System.out.println(map);
    }
}

Output:输出:

{TEST={geocoordinates=[{location=A}, {longitude=B}]}}

Another way of doing it as follows:另一种方法如下:

import java.util.HashMap;

public class Main {
    public static void main(String[] args) {
        HashMap<String, Object> map1 = new HashMap<>();
        map1.put("location", "A");
        map1.put("longitude", "B");

        HashMap<String, Object> map2 = new HashMap<>();
        map2.put("geocoordinates", map1);

        HashMap<String, Object> map = new HashMap<>();
        map.put("TEST", map2);

        System.out.println(map);
    }
}

Output:输出:

{TEST={geocoordinates={location=A, longitude=B}}}

The behavior you're seeing is correct – namely, if you have a map and store a new value for an existing key, the old value will be lost.您看到的行为是正确的 - 即,如果您有一个地图并为现有键存储新值,则旧值将丢失。

Here's a simple example that isolates what you're doing.这是一个简单的例子,它隔离了你正在做的事情。 In this code, the initial value "one" will be overwritten by "two" .在这段代码中,初始值"one"将被"two"覆盖。 This is how maps work.这就是地图的工作方式。 In your case, you're using other maps instead of simple strings, but the behavior is the same – you have one value and you're replacing it with another value.在您的情况下,您使用的是其他映射而不是简单的字符串,但行为是相同的 - 您有一个值,并且正在用另一个值替换它。

HashMap<String, Object> map = new HashMap<>();
map.put("TEST", "one");
map.put("TEST", "two");

To retain both "one" and "two", you need to either use different keys (ex: "TEST1" and "TEST2"), or alter one of the nested maps stored under "TEST", or introduce an altogether different data structure (such as a java.util.Set ).要同时保留“一”和“二”,您需要使用不同的键(例如:“TEST1”和“TEST2”),或者更改存储在“TEST”下的嵌套映射之一,或者引入完全不同的数据结构(例如java.util.Set )。

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

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