I need to merge n hashmaps ideally in a loop like shown below using java 8 merge or something:
Map 1 : {Name:XXX,Phn:123,Work:""}
Map 2 : {Name:XXX,Phn:456,Work: xyz}
Map 3 : {Name:XXX,Phn:789,Work:""}
I would like to get an output like below :
{
Name:XXX, // if all values for a key are same take one
Phn:123/456/789 // merge all non null values for same key
Work:xyz // make sure non-null values are never replaced
}
When I try to use putall like this
public Map<String,String> mergeOriginalDataMaps(List<Integer> indexList, List<Map<String,String>> originalData) {
Map<String,String> tmpMap = new HashMap<String,String> ();
for (int index : indexList ) {
tmpMap.putAll(originalData.get(index));
}
return tmpMap;
}
If the value of duplicate key is "", the previous value is replaced with the new one. I need to concatenate the values rather than replace them.