JAVA语言代码实现数据处理,匹配-批量多文件匹配,文件选择性去重,选择性添加
小标 2018-10-12 来源 : 阅读 1966 评论 0

摘要:本文主要向大家介绍了JAVA语言代码实现数据处理,匹配-批量多文件匹配,文件选择性去重,选择性添加,通过具体的内容向大家展示,希望对大家学习JAVA语言有所帮助。

本文主要向大家介绍了JAVA语言代码实现数据处理,匹配-批量多文件匹配,文件选择性去重,选择性添加,通过具体的内容向大家展示,希望对大家学习JAVA语言有所帮助。


Dataset描述:数据集按照某一列的数据选择性的添加和删除。 选择条件存在另一数据集中。通过遍历比对,重写原文件。


文件命名:原文件 master.*; 比对文件 compare.*;


文件格式:以逗号分隔。如果不是以逗号分隔,可以使用excel 分隔列工具帮助分隔原数据


比对选择条件: 对于一个master中特定比对对象,


如果对象的比对列在compare中存在,则continue,如果不存在,插入compare中的数值,如果存在且不是compare中的数据,删除该对象


举例: master: 匹配条件 compare:筛选项: result:


01, a, b, 0203 a-aa, 0203 0203-0203 01, a, b, 0203


01, a, b, 0204a-a a, 02060204 x01, a, b, 0206


01, a, b, 0205a-a0205 x


02, a, b, 0206a-a0206-0206 02, a, b, 0206


0203 <-- 02, a, b, 0203


03, c, b, 0206 c-a x


实现思想:


将数据分组, 01+a+b 可形成一个unique 组号


将 compare 文件数据 存在 cmp_map 中


将每一组的数据存在 master_map 中


将master_map 中将匹配的所有筛选项存在 match_map中


在一个分组结尾,比对 master_map 和 match_map 生成 record_map (存储在两个文件中都出现的筛选项)


比对 record_map 和 match_map 获取所需要插入master 文件的新筛选项


在操作完成之后清除 master_map, 使用hashMap.clear(); 或在每个group 开头新建一个master_map


如果需要批量处理,在最外层添加文件和文件夹遍历,添加在 2,3 之间


如果对每个文件进行修改,最后使用 .bat 整合成一个完整的csv 文件


部分功能代码块展示:


2. 将compare 数据讯在cmp_map 中


/*Set up the cmp_map*/

 

            /*<test readline="">*/

 

            Map<integer,string> cmp_map = new HashMap<integer,string>();

 

            while((readline_cmp = br_cmp.readLine())!=null) {

 

                String[] array = readline_cmp.split(",");

 

                int id = Integer.parseInt(array[0]);

 

                if(!cmp_map.containsKey(id)) {

 

                    cmp_map.put(id, readline_cmp);

 

                }

 

            }</integer,string></integer,string></test>

   


3.将每一组的数据存在 master_map 中


/*The bound condition is that there are only one member in the group*/ 

                    /*Loop in one group. 

                     *Loop will stop at the first element in the next group*/ 

                    while((readline_master = br.readLine())!=null) {  

                        String cur_key = "";  

                        attr = readline_master.split(",");  

                        /*Get the current key*/ 

                        for(int i=0;i<18;i++) {  

                            cur_key = cur_key + attr[i]+",";  

                        }  

                        /*Using keys to identify whether they belong to the 

                         * same group*/ 

                        if(!cur_key.equals(pre_key)) {  

                            break;  

                        }  

                        master_map.put(readline_master, Integer.parseInt(attr[22]));      

                    }

   


4.将master_map 中将匹配的所有筛选项存在 match_map中


   

/*Set up the map that contains all the compare code (Test ID) that match the 

                     * dir name in master file use the parameter dir_master*/ 

                    Map<string,integer> match_map = new HashMap<string,integer>();  

                    for(Map.Entry<integer, string=""> entry :cmp_map.entrySet()) {  

                        attr = entry.getValue().split(",");  

                        String dir_cmp = attr[6];  

                        if(dir_master.equals(dir_cmp)) {  

                            String cmp_line = pre_key+attr[1]+","+","+","+","+entry.getValue();  

                            match_map.put(cmp_line, Integer.parseInt(attr[0]));  

                        }  

                           

                    }  </integer,></string,integer></string,integer>

   


5.在一个分组结尾,比对 master_map 和 match_map 生成 record_map (存储在两个文件中都出现的筛选项)


/*Record the code that existing in both master and match map*/ 

                    Map<string,integer> record_map = new HashMap<string,integer>();  

                    for(Map.Entry<string,integer> entry_master : master_map.entrySet()) {  

                        for(Map.Entry<string,integer> entry_match : match_map.entrySet()) {  

                            if(entry_master.getValue().equals(entry_match.getValue() )) {  

                                record_map.put(entry_master.getKey(),entry_master.getValue());  

                                writer.write(entry_master.getKey());  

                                writer.newLine();  

                            }  

                        }  

                    }  </string,integer></string,integer></string,integer></string,integer>

   


6.比对 record_map 和 match_map 获取所需要插入master 文件的新筛选项


/*Find the element we need to insert*/ 

                    if(record_map.isEmpty()) {  

                        for(Map.Entry<string,integer> entry_match : match_map.entrySet()) {  

                            writer.write(entry_match.getKey());  

                            writer.newLine();  

                        }  

                    }  

                    else {  

                        for(Map.Entry<string,integer> entry_match : match_map.entrySet()) {  

                            for(Map.Entry<string,integer> entry_record : record_map.entrySet()) {  

                                if(!entry_match.getValue().equals(entry_record.getValue()) ) {  

                                    writer.write(entry_match.getKey());  

                                    writer.newLine();  

                                }  

                            }  

                        }  

                    }  

                       

                }  </string,integer></string,integer></string,integer>

   


          

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注编程语言JAVA频道!


本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 1 不喜欢 | 0
看完这篇文章有何感觉?已经有1人表态,100%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved