Merge Map Java 8 Stream

前端 未结 4 698
灰色年华
灰色年华 2020-12-28 17:56

I would like to merge two Map with JAVA 8 Stream:

Map> mapGlobal = new HashMap>();
Map

        
相关标签:
4条回答
  • 2020-12-28 18:25

    You can do this by iterating over all the entries in mapAdded and merging them into mapGlobal.

    The following iterates over the entries of mapAdded by calling forEach(action) where the action consumes the key and value of each entry. For each entry, we call merge(key, value, remappingFunction) on mapGlobal: this will either create the entry under the key k and value v if the key didn't exist or it will invoke the given remapping function if they already existed. This function takes the 2 lists to merge, which in this case, are first added to a TreeSet to ensure both unique and sorted elements and converted back into a list:

    mapAdded.forEach((k, v) -> mapGlobal.merge(k, v, (v1, v2) -> {
        Set<String> set = new TreeSet<>(v1);
        set.addAll(v2);
        return new ArrayList<>(set);
    }));
    

    If you want to run that potentially in parallel, you can create a Stream pipeline by getting the entrySet() and calling parallelStream() on it. But then, you need to make sure to use a map that supports concurrency for mapGlobal, like a ConcurrentHashMap.

    ConcurrentMap<String, List<String>> mapGlobal = new ConcurrentHashMap<>();
    // ...
    mapAdded.entrySet().parallelStream().forEach(e -> mapGlobal.merge(e.getKey(), e.getValue(), (v1, v2) -> {
        Set<String> set = new TreeSet<>(v1);
        set.addAll(v2);
        return new ArrayList<>(set);
    }));
    
    0 讨论(0)
  • 2020-12-28 18:25

    The original implementation doesn't create result like Map<String, List<Object>>, but Map<String, List<List<String>>>. You need additional Stream pipeline on it to produce Map<String, List<String>>.

    0 讨论(0)
  • 2020-12-28 18:27

    Using foreach over Map can be used to merge given arraylist.

        public Map<String, ArrayList<String>> merge(Map<String, ArrayList<String>> map1, Map<String, ArrayList<String>> map2) {
        Map<String, ArrayList<String>> map = new HashMap<>();
        map.putAll(map1);
    
        map2.forEach((key , value) -> {
            //Get the value for key in map.
            ArrayList<String> list = map.get(key);
            if (list == null) {
                map.put(key,value);
            }
            else {
                //Merge two list together
                ArrayList<String> mergedValue = new ArrayList<>(value);
                mergedValue.addAll(list);
                map.put(key , mergedValue);
            }
        });
        return map;
    }
    
    0 讨论(0)
  • 2020-12-28 18:35

    Using StreamEx

    Map<String, List<String>> mergedMap =
            EntryStream.of(mapGlobal)
                    .append(EntryStream.of(mapAdded))
                    .toMap((v1, v2) -> {
                        List<String> combined = new ArrayList<>();
                        combined.addAll(v1);
                        combined.addAll(v2);
                        return combined;
                    });
    

    If you have even more maps to merge just append to the stream

                    .append(EntryStream.of(mapAdded2))
                    .append(EntryStream.of(mapAdded3))
    
    0 讨论(0)
提交回复
热议问题