-2

below I have two independent map coming which i have stored in individual maps , I want to add them into the third final map, so the final map will contain the sum of maps named m1 and m2 , please advise how to achieve the same

    map1=(Map<String, List<NTM>>) m1.exceute(jobCode, clientlogo);
     map2=(Map<String, List<NTM>>) m1.exceute(jobCode, clientlogo);
private Map<String, List<NTM>> finalMap;

        public Map<String, List<NTM>>  invoke (String jobCode, String clientlogo)
    {
        maestroCardBusinessCNFRuleImpl m1 = new maestroCardBusinessCNFRuleImpl(); 
        maestroCardBusinessANFRuleImpl m2 = new maestroCardBusinessANFRuleImpl();
        try {
            map1=(Map<String, List<NTM>>) m1.exceute(jobCode, clientlogo);
            map2=(Map<String, List<NTM>>) m1.exceute(jobCode, clientlogo);

            // *** logic to add the sum of both the maps ****
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
sdsd dsds
  • 9
  • 4

2 Answers2

2

In Java 7, you could add all entries of the first map in the merged map.
Then iterate on the second map to add entries (as the key is not present) and update entries (as the key is already present) in the merged map.

Map<String,  List<NTM>> mergedMap = new HashMap<>();
mergedMap.putAll(m1);
for (Entry<String, List<NTM>> entry : m2.entrySet()) {
    String key = entry.getKey();
    List<NTM> list = entry.getValue();

    List<NTM> mergedList = mergedMap.get(key);

    // key not existing in the mergedMap
    if (mergedList == null) {
        mergedMap.put(key, list);
    }

    // key existing in the mergedMap
    else {
        mergedList.addAll(list);
    }
}

With Java 8 you could use Collectors.toMap() to provide a merge function that merges the List of NTM as you have duplicate keys in your maps :

Map<String, List<NTM>> mergedMap = Stream.of(m1.entrySet(), m2.entrySet())
                                           .flatMap(Collection::stream)
                                           .collect(Collectors.toMap(Entry::getKey, Entry::getValue, (v1, v2) -> {
                                               v1.addAll(v2);
                                               return v1;
                                           }));

Note that the merge function will change the original list of the map referenced by v1.
If it is not desirable, you could create a new ArrayList and return it such as :

.collect(Collectors.toMap(Entry::getKey, Entry::getValue, (v1, v2) -> {
                                                   List<NTM> mergedList = new 
                                                    ArrayList<>(v1);
                                                   mergedList.addAll(v2);
                                                   return mergedList;
                                               }));
davidxxx
  • 125,838
  • 23
  • 214
  • 215
0
Map<String, List<NTM>> map3 = new HashMap<>();
map3.putAll(map1);
map3.putAll(map2);
Rafał Sokalski
  • 1,817
  • 2
  • 17
  • 29