合并规则:删除家长phone为空的记录,若一个家长对应多个孩子,保留一条家长记录,家长id修改为phone,对应关系也要修改。
代码如下:
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import org.apache.commons.lang3.StringUtils; public class 测试合并_S3_Test { public static void main(String[] args) { 测试合并_S3_Test t = new 测试合并_S3_Test(); t.testMergeData(); } /** * @Description: 删除家长phone为空的记录,若一个家长对应多个孩子,保留一条家长记录,家长id修改为phone,对应关系也要修改 */ public void testMergeData() { List<Map<String, String>> parentList = getMockParentInfoList(); List<Map<String, String>> childList = getMockChildInfoList(); List<Map<String, String>> relaList = getMockRelaInfoList(); System.out.println("----------------合并前------------------"); System.out.println("----------------家长------------------"); for (Map<String, String> parentMap : parentList) { System.out.println(parentMap); } System.out.println("----------------孩子------------------"); for (Map<String, String> childMap : childList) { System.out.println(childMap); } System.out.println("----------------对应关系------------------"); for (Map<String, String> relaMap : relaList) { System.out.println(relaMap); } if (parentList == null || parentList.size() == 0 || childList == null || childList.size() == 0 || relaList == null || relaList.size() == 0) { return; } /** * 按电话号码排序 */ Collections.sort(parentList, new Comparator<Map<String, String>>() { public int compare(Map<String, String> m1, Map<String, String> m2) { return m1.get("phone").compareTo(m2.get("phone")); } }); /** * 找到电话号码相同的家长id并删除电话号码相同的家长 */ Iterator<Map<String, String>> pit = parentList.iterator(); Map<String, List<String>> idMap = new HashMap<String, List<String>>(); List<String> delList = new ArrayList<String>(); boolean isFirst = true; String firstPhone = null, firstId = null; while (pit.hasNext()) { Map<String, String> ptMap = pit.next(); if (StringUtils.isBlank(ptMap.get("phone"))) { delList.add(ptMap.get("id")); pit.remove(); continue; } if (isFirst) { isFirst = false; firstPhone = ptMap.get("phone"); firstId = ptMap.get("id"); } else { if (firstPhone.equals(ptMap.get("phone"))) { if (idMap.containsKey(firstPhone)) { List<String> idList = idMap.get(firstPhone); idList.add(ptMap.get("id")); } else { List<String> idList = new ArrayList<String>(); idList.add(firstId); idList.add(ptMap.get("id")); idMap.put(firstPhone, idList); } pit.remove(); } else { firstPhone = ptMap.get("phone"); firstId = ptMap.get("id"); } } } /** * 删除phone为空的记录 */ List<String> childDelList = new ArrayList<String>(); pit = relaList.iterator(); while (pit.hasNext()) { Map<String, String> relaMap = pit.next(); if (delList.contains(relaMap.get("parentId"))) { childDelList.add(relaMap.get("childId")); pit.remove(); } } pit = childList.iterator(); while (pit.hasNext()) { Map<String, String> childMap = pit.next(); if (childDelList.contains(childMap.get("id"))) { pit.remove(); } } System.out.println("----=" + childList.size()); /** * 修改id为phone */ pit = parentList.iterator(); while (pit.hasNext()) { Map<String, String> ptMap = pit.next(); if (idMap.containsKey(ptMap.get("phone"))) { List<String> idList = idMap.get(ptMap.get("phone")); String phone = ptMap.get("phone"); Iterator<Map<String, String>> relaIt = relaList.iterator(); while (relaIt.hasNext()) { Map<String, String> relaMap = relaIt.next(); if (idList.contains(relaMap.get("parentId"))) { relaMap.put("parentId", phone); } } ptMap.put("id", phone); } } System.out.println("----------------合并后------------------"); System.out.println("----------------家长------------------"); for (Map<String, String> parentMap : parentList) { System.out.println(parentMap); } System.out.println("----------------孩子------------------"); for (Map<String, String> childMap : childList) { System.out.println(childMap); } System.out.println("----------------对应关系------------------"); for (Map<String, String> relaMap : relaList) { System.out.println(relaMap); } } public List<Map<String, String>> getMockParentInfoList() { List<Map<String, String>> parentInfoList = new ArrayList<Map<String, String>>(); Map<String, String> parantInfo = new HashMap<String, String>(); parantInfo.put("id", "123"); parantInfo.put("name", "家长1"); parantInfo.put("account", "4"); parantInfo.put("password", "123"); parantInfo.put("type", "3"); parantInfo.put("phone", "134566666"); parantInfo.put("province", "_省_1"); parantInfo.put("city", "_市_1"); parantInfo.put("userStatus", "1"); parantInfo.put("sex", "1"); parentInfoList.add(parantInfo); Map<String, String> parantInfo2 = new HashMap<String, String>(); parantInfo2.put("id", "125"); parantInfo2.put("name", "家长3"); parantInfo2.put("account", "42"); parantInfo2.put("password", "123"); parantInfo2.put("type", "3"); parantInfo2.put("phone", "134566667"); parantInfo2.put("province", "_省_2"); parantInfo2.put("city", "_市_2"); parantInfo2.put("userStatus", "1"); parantInfo2.put("sex", "1"); parentInfoList.add(parantInfo2); parantInfo2 = new HashMap<String, String>(); parantInfo2.put("id", "129"); parantInfo2.put("name", "家长5"); parantInfo2.put("account", "43"); parantInfo2.put("password", "123"); parantInfo2.put("type", "3"); parantInfo2.put("phone", "134566668"); parantInfo2.put("province", "_省_3"); parantInfo2.put("city", "_市_3"); parantInfo2.put("userStatus", "1"); parantInfo2.put("sex", "1"); parentInfoList.add(parantInfo2); parantInfo2 = new HashMap<String, String>(); parantInfo2.put("id", "130"); parantInfo2.put("name", "家长6"); parantInfo2.put("account", "44"); parantInfo2.put("password", "123"); parantInfo2.put("type", "3"); parantInfo2.put("phone", " "); parantInfo2.put("province", "_省_4"); parantInfo2.put("city", "_市_4"); parantInfo2.put("userStatus", "1"); parantInfo2.put("sex", "1"); parentInfoList.add(parantInfo2); parantInfo2 = new HashMap<String, String>(); parantInfo2.put("id", "124"); parantInfo2.put("name", "家长2"); parantInfo2.put("account", "45"); parantInfo2.put("password", "123"); parantInfo2.put("type", "3"); parantInfo2.put("phone", "134566666"); parantInfo2.put("province", "_省_5"); parantInfo2.put("city", "_市_5"); parantInfo2.put("userStatus", "1"); parantInfo2.put("sex", "1"); parentInfoList.add(parantInfo2); parantInfo2 = new HashMap<String, String>(); parantInfo2.put("id", "128"); parantInfo2.put("name", "家长4"); parantInfo2.put("account", "46"); parantInfo2.put("password", "123"); parantInfo2.put("type", "3"); parantInfo2.put("phone", "134566668"); parantInfo2.put("province", "_省_6"); parantInfo2.put("city", "_市_6"); parantInfo2.put("userStatus", "1"); parantInfo2.put("sex", "1"); parentInfoList.add(parantInfo2); return parentInfoList; } public List<Map<String, String>> getMockChildInfoList() { List<Map<String, String>> childrenInfoList = new ArrayList<Map<String, String>>(); Map<String, String> childInfo = new HashMap<String, String>(); childInfo.put("id", "123"); childInfo.put("name", "孩子1"); childInfo.put("account", "47"); childInfo.put("password", "123"); childInfo.put("type", "2"); childInfo.put("province", "_省_1"); childInfo.put("city", "_市_1"); childInfo.put("userStatus", "1"); childInfo.put("sex", "1"); childrenInfoList.add(childInfo); Map<String, String> childInfo2 = new HashMap<String, String>(); childInfo2.put("id", "125"); childInfo2.put("name", "孩子2"); childInfo2.put("account", "48"); childInfo2.put("password", "123"); childInfo2.put("type", "2"); childInfo2.put("province", "_省_2"); childInfo2.put("city", "_市_2"); childInfo2.put("userStatus", "1"); childInfo2.put("sex", "1"); childrenInfoList.add(childInfo2); childInfo2 = new HashMap<String, String>(); childInfo2.put("id", "131"); childInfo2.put("name", "孩子6"); childInfo2.put("account", "49"); childInfo2.put("password", "123"); childInfo2.put("type", "2"); childInfo2.put("province", "_省_3"); childInfo2.put("city", "_市_3"); childInfo2.put("userStatus", "1"); childInfo2.put("sex", "1"); childrenInfoList.add(childInfo2); childInfo2 = new HashMap<String, String>(); childInfo2.put("id", "129"); childInfo2.put("name", "孩子5"); childInfo2.put("account", "50"); childInfo2.put("password", "123"); childInfo2.put("type", "2"); childInfo2.put("province", "_省_4"); childInfo2.put("city", "_市_4"); childInfo2.put("userStatus", "1"); childInfo2.put("sex", "1"); childrenInfoList.add(childInfo2); childInfo2 = new HashMap<String, String>(); childInfo2.put("id", "126"); childInfo2.put("name", "孩子3"); childInfo2.put("account", "51"); childInfo2.put("password", "123"); childInfo2.put("type", "2"); childInfo2.put("province", "_省_5"); childInfo2.put("city", "_市_5"); childInfo2.put("userStatus", "1"); childInfo2.put("sex", "1"); childrenInfoList.add(childInfo2); childInfo2 = new HashMap<String, String>(); childInfo2.put("id", "128"); childInfo2.put("name", "孩子4"); childInfo2.put("account", "52"); childInfo2.put("password", "123"); childInfo2.put("type", "2"); childInfo2.put("province", "_省_6"); childInfo2.put("city", "_市_6"); childInfo2.put("userStatus", "1"); childInfo2.put("sex", "1"); childrenInfoList.add(childInfo2); return childrenInfoList; } public List<Map<String, String>> getMockRelaInfoList() { List<Map<String, String>> relaInfoList = new ArrayList<Map<String, String>>(); Map<String, String> relaMap = new HashMap<String, String>(); relaMap.put("parentId", "123"); relaMap.put("childId", "123"); relaInfoList.add(relaMap); relaMap = new HashMap<String, String>(); relaMap.put("parentId", "130"); relaMap.put("childId", "131"); relaInfoList.add(relaMap); relaMap = new HashMap<String, String>(); relaMap.put("parentId", "124"); relaMap.put("childId", "125"); relaInfoList.add(relaMap); relaMap = new HashMap<String, String>(); relaMap.put("parentId", "125"); relaMap.put("childId", "126"); relaInfoList.add(relaMap); relaMap = new HashMap<String, String>(); relaMap.put("parentId", "128"); relaMap.put("childId", "128"); relaInfoList.add(relaMap); relaMap = new HashMap<String, String>(); relaMap.put("parentId", "129"); relaMap.put("childId", "129"); relaInfoList.add(relaMap); return relaInfoList; } }
结果为:
----------------合并前------------------ ----------------家长------------------ {password=123, userStatus=1, province=_省_1, phone=134566666, city=_市_1, sex=1, name=家长1, id=123, type=3, account=4} {password=123, userStatus=1, province=_省_2, phone=134566667, city=_市_2, sex=1, name=家长3, id=125, type=3, account=42} {password=123, userStatus=1, province=_省_3, phone=134566668, city=_市_3, sex=1, name=家长5, id=129, type=3, account=43} {password=123, userStatus=1, province=_省_4, phone= , city=_市_4, sex=1, name=家长6, id=130, type=3, account=44} {password=123, userStatus=1, province=_省_5, phone=134566666, city=_市_5, sex=1, name=家长2, id=124, type=3, account=45} {password=123, userStatus=1, province=_省_6, phone=134566668, city=_市_6, sex=1, name=家长4, id=128, type=3, account=46} ----------------孩子------------------ {password=123, userStatus=1, province=_省_1, city=_市_1, sex=1, name=孩子1, id=123, type=2, account=47} {password=123, userStatus=1, province=_省_2, city=_市_2, sex=1, name=孩子2, id=125, type=2, account=48} {password=123, userStatus=1, province=_省_3, city=_市_3, sex=1, name=孩子6, id=131, type=2, account=49} {password=123, userStatus=1, province=_省_4, city=_市_4, sex=1, name=孩子5, id=129, type=2, account=50} {password=123, userStatus=1, province=_省_5, city=_市_5, sex=1, name=孩子3, id=126, type=2, account=51} {password=123, userStatus=1, province=_省_6, city=_市_6, sex=1, name=孩子4, id=128, type=2, account=52} ----------------对应关系------------------ {childId=123, parentId=123} {childId=131, parentId=130} {childId=125, parentId=124} {childId=126, parentId=125} {childId=128, parentId=128} {childId=129, parentId=129} ----=5 ----------------合并后------------------ ----------------家长------------------ {password=123, userStatus=1, province=_省_1, phone=134566666, city=_市_1, sex=1, name=家长1, id=134566666, type=3, account=4} {password=123, userStatus=1, province=_省_2, phone=134566667, city=_市_2, sex=1, name=家长3, id=125, type=3, account=42} {password=123, userStatus=1, province=_省_3, phone=134566668, city=_市_3, sex=1, name=家长5, id=134566668, type=3, account=43} ----------------孩子------------------ {password=123, userStatus=1, province=_省_1, city=_市_1, sex=1, name=孩子1, id=123, type=2, account=47} {password=123, userStatus=1, province=_省_2, city=_市_2, sex=1, name=孩子2, id=125, type=2, account=48} {password=123, userStatus=1, province=_省_4, city=_市_4, sex=1, name=孩子5, id=129, type=2, account=50} {password=123, userStatus=1, province=_省_5, city=_市_5, sex=1, name=孩子3, id=126, type=2, account=51} {password=123, userStatus=1, province=_省_6, city=_市_6, sex=1, name=孩子4, id=128, type=2, account=52} ----------------对应关系------------------ {childId=123, parentId=134566666} {childId=125, parentId=134566666} {childId=126, parentId=125} {childId=128, parentId=134566668} {childId=129, parentId=134566668}
全文完。