//比较两个实体,返回两个Map结果 public static void compare(String tableId, Object source, Object target, Map sourceMap, Map targetMap) { sourceMap.putAll((Map) BeanMapPlus(BeanUtil.beanToMap(source))); targetMap.putAll((Map) BeanMapPlus(BeanUtil.beanToMap(target))); delIntersection(tableId, sourceMap, targetMap); } ; //beantoMap加强,完全Map化 private static Object BeanMapPlus(Object map) { if (map instanceof Map) { HashSet<Object> set = new HashSet<>(); set.addAll(((Map) map).keySet()); for (Object o : set) { if (ObjectUtils.isEmpty(((Map) map).get(o))) {((Map<?, ?>) map).remove(o);continue; } if (BeanUtil.isBean(((Map) map).get(o).getClass()) && !(((Map) map).get(o) instanceof Date)) { ((Map) map).put(o, BeanMapPlus(BeanUtil.beanToMap(((Map) map).get(o)))); } if (((Map) map).get(o) instanceof Collection) BeanMapPlus(((Collection<?>) ((Map) map).get(o))); } } else if (map instanceof Collection && !ObjectUtils.isEmpty(map)) { List list = new ArrayList<>(); for (Object o : ((Collection<?>) map)) { list.add(BeanMapPlus(BeanUtil.beanToMap(o))); } ((Collection<?>) map).clear(); ((Collection<?>) map).addAll(list); } return map; } //删除两个Map的键值对交集 private static void delIntersection(String tableId, Map sourceMap, Map targetMap) { HashSet<Object> set = new HashSet<>(); set.addAll(sourceMap.keySet()); for (Object o : set) { if (!targetMap.containsKey(o)) continue; if (targetMap.get(o) instanceof Map && sourceMap.get(o) instanceof Map) delIntersection(tableId, (Map) sourceMap.get(o), (Map) targetMap.get(o)); if (targetMap.get(o) instanceof Collection && sourceMap.get(o) instanceof Collection) { if (((Collection<?>) targetMap.get(o)).containsAll(((Collection<?>) sourceMap.get(o)))) { targetMap.remove(o); sourceMap.remove(o); continue; } //按数组顺序比较 ArrayList<Object> soruce = new ArrayList<>(); soruce.addAll(((Collection<?>) sourceMap.get(o))); ArrayList<Object> target = new ArrayList<>(); target.addAll(((Collection<?>) targetMap.get(o))); for (int i = 0; i < Math.min(soruce.size(),target.size()); i++) { if(soruce.get(i) instanceof Map && target.get(i) instanceof Map) delIntersection(tableId,(Map) soruce.get(i),(Map)target.get(i)); } // //按 tableId 比较 // List list = new ArrayList(); // for (Object o1 : ((Collection<?>) targetMap.get(o))) { // for (Object o2 : ((Collection<?>) sourceMap.get(o))) { // if (o1 == o2) list.add(o1); // else if (o1 instanceof Map // && o2 instanceof Map // && ((Map) o1).containsKey(tableId) // && ((Map) o2).containsKey(tableId) // && ((Map) o1).get(tableId) == ((Map) o2).get(tableId) // ) delIntersection(tableId, ((Map) o1), ((Map) o2)); // } // } // if (!ObjectUtils.isEmpty(list)) { // ((Collection<?>) targetMap.get(o)).removeAll(list); // ((Collection<?>) sourceMap.get(o)).removeAll(list); // } } if (targetMap.get(o).getClass() != sourceMap.get(o).getClass()) continue; if (targetMap.get(o).toString().equals(sourceMap.get(o).toString())) { targetMap.remove(o); sourceMap.remove(o); continue; } } }
注:
BeanMapPlus这个方法目前只排除了Date类型,如果由其他需要排除的属性类型,可以再调整;标签:map,实体类,get,不同,Collection,sourceMap,Map,对比,targetMap From: https://www.cnblogs.com/tangzeqi/p/18534743