使用每个键的多个值正确合并2个或更多Hashmaps

时间:2018-04-06 20:46:52

标签: java android json hashmap

我正在寻找一种在1中正确合并2个或更多个hashmaps的方法,在这个特定情况下我想要将2个或更多个hashmaps与许多键合并,在这种情况下,他们各自的值大于1. like(key (i),value1,value2,... valueN)

我的代码:

int f =0;

while (f < Response.size()) {

map3.putAll(Response.get(f).getUsers());

f = f + 1;}

其中Response是我保存json的类,Users是我在类中定义的HashMap,当然在Map Users中我有getters来映射诸如users.getName,users.getDescription,users之类的值。 getAge

代码:

public class Response {

 private Map<String, Users> users = new LinkedHashMap<>();

 public Map<String, ProductsBean> getusers() {return users;}

  public static class Users {

        private String name;
        private String description;
        private String age;

      public String getName() {return name;}
      public String getDescription() {return description;}
      public String getAge() {return age;} }

我需要合并我需要发送到适配器的所有地图。

1 个答案:

答案 0 :(得分:0)

我不确定我是否正确理解您的代码,因为getUsers看起来不会返回Map

但是,我可以帮助您合并两个地图,如下所示:

import static org.junit.Assert.assertEquals;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.junit.Test;

public class ExampleTest {

    @Test
    public void test() {

        // Create maps to combine
        Map<String, String> mapA = new LinkedHashMap<>();
        Map<String, String> mapB = new LinkedHashMap<>();

        // Create final map
        Map<String, List<String>> mapC = new LinkedHashMap<>();

        // Add data to input mapA
        mapA.put("A", "value1");
        mapA.put("B", "value2");

        // Add data to input mapB
        mapB.put("C", "Happy");
        mapB.put("B", "Sad");

        // Fill results from mapA
        mapC = populateMapCWithMapContent(mapC, mapA);
        mapC = populateMapCWithMapContent(mapC, mapB);

        // Check Results
        assertEquals(2, mapA.size());
        assertEquals(2, mapB.size());
        assertEquals(3, mapC.size());

        // show results
        for (String key : mapC.keySet()) {
            System.out.println("Key=" + key);
            List<String> values = mapC.get(key);
            for (String value : values) {
                System.out.println("    Value = " + value);
            }
        }

    }

    /**
     * Populate mapC with provided map Contents
     * 
     * @param mapC
     *            - resulting map
     * @param map
     *            - input data
     * @return mapC
     * 
     *         Note: this would work with a void, but I always feel that if mapC is
     *         changed, then mapC should be returned (To help readability). 
     */
    private Map<String, List<String>> populateMapCWithMapContent(Map<String, List<String>> mapC,
            Map<String, String> inputMap) {
        List<String> mapCValues;
        for (String key : inputMap.keySet()) {
        if (mapC.containsKey(key)) {
                mapCValues = mapC.get(key);
            } else {
                mapCValues = new ArrayList<String>();
            }
            mapCValues.add(inputMap.get(key));
            mapC.put(key, mapCValues);
        }
        return mapC;
    }
}

其中提供了以下结果:

Key=A
    Value = value1
Key=B
    Value = value2 
    Value = Sad
Key=C 
    Value = Happy

请注意,mapA和mapB只包含String值,但由于组合映射(mapC)需要为单个键保存多个值,因此我使用了List。 也可以包含另一个地图而不是字符串。 e.g。

Map<String, LinkedHashMap<String,Object>> mapC = new LinkedHashMap<>();