map差异对比工具类
作者:互联网
/*Map差异对比工具类/
public class MapThenUtils {
public static List<String> getKeys(Map<String,Object> oldMap,Map<String,Object> newMap){
Set<String> oldSet = oldMap.keySet();
Set<String> newSet = newMap.keySet();
Iterator<String> oldIterator = oldSet.iterator();
Iterator<String> newIterator = newSet.iterator();
List<String> oldKeys = new ArrayList<>();
List<String> newKeys = new ArrayList<>();
while(oldIterator.hasNext()){
oldKeys.add(oldIterator.next());
}
while(newIterator.hasNext()){
newKeys.add(newIterator.next());
}
return Stream.of(oldKeys,newKeys).flatMap(Collection::stream).distinct().collect(Collectors.toList());
}
public static List<Map<String,Object>> compareInstance(Map<String,Object> oldMap,Map<String,Object> newMap){
List<Map<String,Object>> comparbleResults = new ArrayList<>();
List<String> keys = getKeys(oldMap, newMap);
for (String key : keys) {
Map<String,Object> comparbleResult = new HashMap<>();
Object oldValue = oldMap.get(key);
Object newValue = newMap.get(key);
if(StringUtils.isEmpty(String.valueOf(oldValue))&& StringUtils.isEmpty(String.valueOf(newValue))){
continue;
}
if(StringUtils.isEmpty(String.valueOf(oldValue)) || StringUtils.isEmpty(String.valueOf(newValue)) || !String.valueOf(oldValue).equals(String.valueOf(newValue))){
comparbleResult.put("name",key);
comparbleResult.put("oldValue",oldValue);
comparbleResult.put("newValue",newValue);
comparbleResults.add(comparbleResult);
}
}
return comparbleResults;
}
}
标签:map,String,Map,差异,valueOf,List,newValue,oldValue,对比 来源: https://blog.csdn.net/a15906449458/article/details/123175594