对二级数据统计并返回算法

2019-02-01  本文已影响6人  墨色尘埃

有这样一组数据,需要将数据中的二级目录查询出来,并对二级目录的次数进行统计


image.png

最后的结构如下:

{
    "data": [
        {
            "fullname": "办公室", 
            "fullcode": "32010002", 
            "registerNum": "0", 
            "verifyNum": "0", 
            "verifyNotNum": "0", 
            "deleteNum": "0"
        }, 
        {
            "fullname": "人力资源部", 
            "fullcode": "32010003", 
            "registerNum": "0", 
            "verifyNum": "0", 
            "verifyNotNum": "0", 
            "deleteNum": "0"
        }, 

              ..............

        {
            "fullname": "高淳区分公司", 
            "fullcode": "320125", 
            "registerNum": "0", 
            "verifyNum": "0", 
            "verifyNotNum": "0", 
            "deleteNum": "0"
        }
    ], 
    "error": null
}

方法①

对于这样的数据结构,应该想到使用Map来做,key为fullName中的二级目录+fullCode中的二级目录,即key="办公室|32010002"

 List<TotalInfo> eligibleInfo = appInfoMapper.getEligibleDetail(startTime, endTime, fullCode);

 Map<String, TotalInfo> result = new LinkedHashMap<>();
 for (TotalInfo totalInfo : eligibleInfo) {
     String key = totalInfo.getFullname().split("/")[1] + "|" + totalInfo.getFullcode().split("/")[1];
     if (result.containsKey(key)) {
         Integer registerNum = Integer.valueOf(totalInfo.getRegisterNum());
         Integer registerNum1 = Integer.valueOf(result.get(key).getRegisterNum());
         registerNum1 += registerNum;
         result.get(key).setRegisterNum(registerNum1 + "");

         Integer verifyNum = Integer.valueOf(totalInfo.getVerifyNum());
         Integer verifyNum1 = Integer.valueOf(result.get(key).getVerifyNum());
         verifyNum1 += verifyNum;
         result.get(key).setVerifyNum(verifyNum1 + "");

         Integer verifyNotNum = Integer.valueOf(totalInfo.getVerifyNotNum());
         Integer verifyNotNum1 = Integer.valueOf(result.get(key).getVerifyNotNum());
         verifyNotNum1 += verifyNotNum;
         result.get(key).setVerifyNotNum(verifyNotNum1 + "");

         Integer deleteNum = Integer.valueOf(totalInfo.getDeleteNum());
         Integer deleteNum1 = Integer.valueOf(result.get(key).getDeleteNum());
         deleteNum1 += deleteNum;
         result.get(key).setDeleteNum(deleteNum1 + "");
     } else {
         TotalInfo newTest = new TotalInfo();
         newTest.setFullname(totalInfo.getFullname().split("/")[1]);
         newTest.setFullcode(totalInfo.getFullcode().split("/")[1]);
         newTest.setRegisterNum(totalInfo.getRegisterNum());
         newTest.setVerifyNum(totalInfo.getVerifyNum());
         newTest.setVerifyNotNum(totalInfo.getVerifyNotNum());
         newTest.setDeleteNum(totalInfo.getDeleteNum());
         result.put(key, newTest);
     }
 }

 List<TotalInfo> list = new ArrayList<>();
 list.addAll(result.values()); // TODO: 2019/1/31
 return new ResponseObj<>(list, null);  

方法②

使用双重for循环,但是随着结果集result的数据越来越多,循环的次数也越来越多,相比于第一种方法,时间复杂度是o(n)。第一种方法只需要对Map进行遍历即可

        //元数据
        List<TotalInfo> list = new ArrayList();
        //结果
        List<TotalInfo> result = new ArrayList<>();
        for (TotalInfo source : list) {
            boolean isFind = false;
            for (TotalInfo dest : result) {
                if (source.getFullname().split("/")[1] == dest.getFullname().split("/")[1] && source != dest) {

                    Integer reportNum = Integer.valueOf(source.getReportNum());
                    Integer reportNum1 = Integer.valueOf(dest.getReportNum());
                    reportNum1 += reportNum;
                    dest.setReportNum(reportNum1 + "");

                    Integer appReportNum = Integer.valueOf(source.getAppReportNum());
                    Integer appReportNum1 = Integer.valueOf(dest.getAppReportNum());
                    appReportNum1 += reportNum;
                    dest.setAppReportNum(appReportNum1 + "");

                    isFind = true;
                }
            }
            if (!isFind) {
                result.add(source);
            }
        }
        return result;

TotalInfo

package zhab.model;

import com.fasterxml.jackson.annotation.JsonInclude;

/**
 * Created by hjh on 2019/1/13.
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
public class TotalInfo {
    private String fullname;
    private String fullcode;
    private String num;
    private String report;


    private String reportNum; //上报数量
    private String appReportNum; //App上报数量
    private String rectifyPdNum; //整改派单
    private String rectifyZhNum; //整改中
    private String rectifyWcNum; //整改完成

    private String publishNum;//信息发布
    private String appPublishNum;//App信息发布

    private String registerNum;//注册人数
    private String verifyNum;//审核通过人数
    private String verifyNotNum;//未审核人数
    private String deleteNum;//删除人数

    public String getAppPublishNum() {
        return appPublishNum;
    }

    public void setAppPublishNum(String appPublishNum) {
        this.appPublishNum = appPublishNum;
    }

    public String getReportNum() {
        return reportNum;
    }

    public void setReportNum(String reportNum) {
        this.reportNum = reportNum;
    }

    public String getRegisterNum() {
        return registerNum;
    }

    public void setRegisterNum(String registerNum) {
        this.registerNum = registerNum;
    }

    public String getVerifyNum() {
        return verifyNum;
    }

    public void setVerifyNum(String verifyNum) {
        this.verifyNum = verifyNum;
    }

    public String getVerifyNotNum() {
        return verifyNotNum;
    }

    public void setVerifyNotNum(String verifyNotNum) {
        this.verifyNotNum = verifyNotNum;
    }

    public String getDeleteNum() {
        return deleteNum;
    }

    public void setDeleteNum(String deleteNum) {
        this.deleteNum = deleteNum;
    }


    public String getPublishNum() {
        return publishNum;
    }

    public void setPublishNum(String publishNum) {
        this.publishNum = publishNum;
    }

    public String getAppReportNum() {
        return appReportNum;
    }

    public void setAppReportNum(String appReportNum) {
        this.appReportNum = appReportNum;
    }

    public String getRectifyPdNum() {
        return rectifyPdNum;
    }

    public void setRectifyPdNum(String rectifyPdNum) {
        this.rectifyPdNum = rectifyPdNum;
    }

    public String getRectifyZhNum() {
        return rectifyZhNum;
    }

    public void setRectifyZhNum(String rectifyZhNum) {
        this.rectifyZhNum = rectifyZhNum;
    }

    public String getRectifyWcNum() {
        return rectifyWcNum;
    }

    public void setRectifyWcNum(String rectifyWcNum) {
        this.rectifyWcNum = rectifyWcNum;
    }

    public String getReport() {
        return report;
    }

    public void setReport(String report) {
        this.report = report;
    }

    public String getFullname() {
        return fullname;
    }

    public void setFullname(String fullname) {
        this.fullname = fullname;
    }

    public String getFullcode() {
        return fullcode;
    }

    public void setFullcode(String fullcode) {
        this.fullcode = fullcode;
    }


    public String getNum() {
        return num;
    }

    public void setNum(String num) {
        this.num = num;
    }
}


上一篇下一篇

猜你喜欢

热点阅读