`
53873039oycg
  • 浏览: 824230 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

[简单]代码片段_数据合并

    博客分类:
  • java
 
阅读更多

        合并规则:删除家长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}

 

   全文完。

分享到:
评论

相关推荐

    论文研究-基于演化模式克隆代码Bugs倾向性分析.pdf

    首先使用自主研发工具CloneCodeTracker跟踪克隆演化,然后结合克隆代码Bugs修复数据分视角讨论不同演化模式下克隆代码Bugs倾向性。经过对12款软件近2万个版本实验,结果表明:克隆群视角下复杂演化模式Bugs倾向性...

    重构_改善既有代码的设计[高清版]中文版

     Consolidate Duplicate Conditional Fragments 合并重复的条件片段   Remove Control Flag 去除控制标志   Replace Nested Conditional with Guard Clauses 用守卫语句代替嵌套条件语句   Replace ...

    重构_改善既有代码的设计.pdf

    9.3 ConsolidateDuplicateConditionalFragments(合并重复的条件片段)243 9.4 RemoveControlFlag(移除控制标记)245 9.5 ReplaceNestedConditionalwithGuardClauses(以卫语句取代嵌套条件表达式)250 9.6 ...

    重构-改善既有代码的设计 中文版.pdf

    9.3 Co olidate Duplicate Conditional Fragments (合并重复的条件片段) 9.4 Remove Control Flag(移除控制标记) 9.5 Replace Nested Conditional with Guard Clauses (以卫语句取代嵌套条件式) 9.6 Replace ...

    算法:数据结构和算法必知必会的50个代码实现

    数据结构和算法必知必会的50个代码实现 微信搜索我的公众号“小争哥”,或者微信扫描以下二维码关注 关注微信公众号,回复“ PDF”获取独家算法资料。 前Google工程师,10万人跟着学的《数据结构和算法之美》《设计...

    Oracle SQL高级编程(资深Oracle专家力作,OakTable团队推荐)--随书源代码

    该资料是《Oracle SQL高级编程》的源代码 对应的书籍资料见: Oracle SQL高级编程(资深Oracle专家力作,OakTable团队推荐) 基本信息 原书名: Pro Oracle SQL 原出版社: Apress 作者: (美)Karen Morton Kerry ...

    重构_改善既有代码的设计

     9.3 ConsolidateDuplicateConditionalFragments(合并重复的条件片段)243  9.4 RemoveControlFlag(移除控制标记)245  9.5 ReplaceNestedConditionalwithGuardClauses(以卫语句取代嵌套条件表达式)250  9.6...

    重构:改善既有代码的设计(中文高清版)

    9.3 ConsolidateDuplicateConditionalFragments(合并重复的条件片段)243 9.4 RemoveControlFlag(移除控制标记)245 9.5 ReplaceNestedConditionalwithGuardClauses(以卫语句取代嵌套条件表达式)250 9.6 ...

    Coursera_3_GettingAndCleaningData-:获取和清理数据课程 - 作业

    您可以在下面找到代码片段和详细注释,以明确说明该步骤的功能。 1. 合并训练集和测试集以创建一个数据集。 x_train &lt;- read.table("train/X_train.txt", sep = " "); x_test &lt;- read.table("test/X_test....

    MyDog是一个代码生成工具.zip

    提供集成开发环境(IDE),如Visual Studio、Eclipse、Android Studio和Sublime Text等,这些工具集成了文本编辑器,支持语法高亮、自动补全、代码片段管理和版本控制等功能,有助于开发者高效编写和维护代码。...

    重建——改善既有代码的设计

    9.3 ConsolidateDuplicateConditionalFragments(合并重复的条件片段)243 9.4 RemoveControlFlag(移除控制标记)245 9.5 ReplaceNestedConditionalwithGuardClauses(以卫语句取代嵌套条件表达式)250 9.6 ...

    JEECG是一款基于代码生成器的J2EE快速开发平台.zip

    提供集成开发环境(IDE),如Visual Studio、Eclipse、Android Studio和Sublime Text等,这些工具集成了文本编辑器,支持语法高亮、自动补全、代码片段管理和版本控制等功能,有助于开发者高效编写和维护代码。...

    一款Android快速开发框(常用工具箱,控件,刷新,数据适配器).zip

    提供集成开发环境(IDE),如Visual Studio、Eclipse、Android Studio和Sublime Text等,这些工具集成了文本编辑器,支持语法高亮、自动补全、代码片段管理和版本控制等功能,有助于开发者高效编写和维护代码。...

    重构:改善既有代码的设计(中文版).

    9.3 ConsolidateDuplicateConditionalFragments(合并重复的条件片段)243 9.4 RemoveControlFlag(移除控制标记)245 9.5 ReplaceNestedConditionalwithGuardClauses(以卫语句取代嵌套条件表达式)250 9.6 ...

    最新 Devart dbForge Studio for SQL Server Enterprise 5.5破解版

    受益于Intellisense类似的功能开箱即用 智能代码完成,代码格式化,SQL重构,片段,配置文件,语句扩展,列选择器等等! 处理模式/数据差异没有麻烦 比较不同SQLServer实例中的数据并生成同步脚本。 管理源代码管理...

    重构-改善既有代码的设计

    9.3 Consolidate Duplicate Conditional Fragments(合并重复的条件片段) 243 9.4 Remove Control Flag(移除控制标记) 245 9.5 Replace Nested Conditional with Guard Clauses(以卫语句取代嵌套条件表达式...

    ThinkAndroid是简易的、遵循Apache2开源协议发布的Android开发框架.zip

    提供集成开发环境(IDE),如Visual Studio、Eclipse、Android Studio和Sublime Text等,这些工具集成了文本编辑器,支持语法高亮、自动补全、代码片段管理和版本控制等功能,有助于开发者高效编写和维护代码。...

    Devart dbForge Studio for SQL Server Enterprise 破解版

     智能代码完成,代码格式化,SQL重构,片段,配置文件,语句扩展,列选择器等等!  处理模式/数据差异没有麻烦  比较不同SQLServer实例中的数据并生成同步脚本。  管理源代码管理中的数据库更改  做一个...

    Devart dbForge Studio for SQL Server v5.2.177 Enterprise.和谐版

     智能代码完成,代码格式化,SQL重构,片段,配置文件,语句扩展,列选择器等等!  处理模式/数据差异没有麻烦  比较不同SQLServer实例中的数据并生成同步脚本。  管理源代码管理中的数据库更改  做一个...

    Devart dbForge Studio for SQL Server v5.2.177 Enterprise 破解版

    受益于Intellisense类似的功能开箱即用 智能代码完成,代码格式化,SQL重构,片段,配置文件,语句扩展,列选择器等等! 处理模式/数据差异没有麻烦 比较不同SQLServer实例中的数据并生成同步脚本。 管理源代码管理...

Global site tag (gtag.js) - Google Analytics