加入java中的地图对象列表

时间:2017-02-28 09:46:30

标签: java arraylist collections hashmap

有两个Map列表,应该在这两个列表上做数据集连接,leftJoin,rightJoin,outerJoin和innerJoin

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

public class JoinMap {
    public static List<Map<String, Object>> getFirstMap() {
        List<Map<String,  Object>> data = new ArrayList<>();
        Map<String,  Object> dataMap2 = new HashMap<>();
        dataMap2.put("user", "vs");
        dataMap2.put("data1", 12.0);
        dataMap2.put("data2", 42.0);
        dataMap2.put("data3", 32.0);
        data.add(dataMap2);
        Map<String,  Object> dataMap1 = new HashMap<>();
        dataMap1.put("user", "samraj");
        dataMap1.put("data1", 11.0);
        dataMap1.put("data2", 41.0);
        dataMap1.put("data3", 31.0);
        data.add(dataMap1);
        Map<String,  Object> dataMap = new HashMap<>();
        dataMap.put("user", "varghees");
        dataMap.put("data1", 10.0);
        dataMap.put("data2", 40.0);
        dataMap.put("data3", 30.0);
        data.add(dataMap);
        return data;
    }

    public static List<Map<String, Object>> getSecondMap() {
        List<Map<String,  Object>> data = new ArrayList<>();
        Map<String,  Object> dataMap2 = new HashMap<>();
        dataMap2.put("user", "vs");
        dataMap2.put("test1", 12.0);
        dataMap2.put("test2", 42.0);
        dataMap2.put("test3", 32.0);
        data.add(dataMap2);
        Map<String,  Object> dataMap1 = new HashMap<>();
        dataMap1.put("user", "samraj");
        dataMap1.put("test1", 11.0);
        dataMap1.put("test2", 41.0);
        dataMap1.put("test3", 31.0);
        data.add(dataMap1);
        Map<String,  Object> dataMap = new HashMap<>();
        dataMap.put("user", "varghees");
        dataMap.put("test1", 10.0);
        dataMap.put("test2", 40.0);
        dataMap.put("test3", 30.0);
        data.add(dataMap);
        return data;
    }

    public static List<Map<String, Object>> leftJoin(List<Map<String, Object>> map1, List<Map<String, Object>> map2) {

    }
    public static List<Map<String, Object>> rightJoin(List<Map<String, Object>> map1, List<Map<String, Object>> map2) {

    }
    public static List<Map<String, Object>> innerJoin(List<Map<String, Object>> map1, List<Map<String, Object>> map2) {

    }
    public static List<Map<String, Object>> outerJoin(List<Map<String, Object>> map1, List<Map<String, Object>> map2) {

    }

    public static void main(String[] argv) {
        List<Map<String, Object>> firstMap = getFirstMap();
        List<Map<String, Object>> secondMap = getSecondMap();
    }
}

Java8中是否有任何简单的函数可以实现这些方法?我试图避免迭代数据并填充新的数据集。

1 个答案:

答案 0 :(得分:2)

当然有。这是一个使用内部连接流的示例。我假设我们知道我们应该加入"user"(如果没有,它会变得更复杂,但仍然可以完成)。

public static List<Map<String, Object>> innerJoin(List<Map<String, Object>> map1, List<Map<String, Object>> map2) {
    return map1.stream().flatMap(m1 -> 
        map2.stream()
                .filter(m2 -> m1.get("user").equals(m2.get("user")))
                .map((Map<String, Object> m2) -> {
                    Map<String, Object> mr = new HashMap<>();
                    mr.putAll(m1);
                    mr.putAll(m2);
                    return mr;
                })
    ).collect(Collectors.toList());
}

我希望并且认为你能够获得灵感并自己做其他人。