对List<Map<String,String>>数据重新组装成所需对象数组格式

个人工作中知识点记录,以便加深影响

1.需求:需要实现一个echarts叠堆报表,如图:

所需格式:

// 指定图表的配置项和数据
    var option = {
            toolbox: {
                show : true,
                feature : {
                     magicType : {show: true, type: ['line', 'bar']},
                    dataView : {show: false, readOnly: false},                               
                    restore : {show: false},
                    saveAsImage : {show: true},
                    myTool : {
                        show : true,
                        title : 'excel下载',
                        icon : 'image://../../resource/images/op/base/exl.png',
                        onclick : function (){
                            location.href=encodeURI(getRootPath_web()+"/opMain/exportExcel.xhtml?title=人脸识别业务量&colnames=公司名称,50分以下,50(含)-55分,55(含)-60分,60(含)-65分,65(含)-70分,70(含)-75分,75(含)-80分,80(含)-85分,85(含)以上,其它&datalist="+JSON.stringify(exlArrs.sort()));
                        }
                    }
                }
            },
             tooltip : {
                 trigger: 'axis'
             },
             grid: {
               bottom: 200,
           },
             xAxis : [
                 {
                     type : 'category',
                     axisLabel: {rotate: 30,interval:-1},
                     flag: dataObj.flag,
                     data : dataObj.gsName,
                     fid: dataObj.gsId
                 }
             ],
             yAxis : [
                 {
                     type : 'value'
                 }
             ],
            dataZoom: [
               {
                   show: true,
                   showDetail: false,
                   bottom:'50',
                   start: 1,
                   end: 50
               },
           ],
             series : 
                 [
                    {
                        name: '50分以下',
                        type: 'bar',
                        stack: '总量',
                        /*label: {
                            normal: {
                                show: true,
                                position: 'insideRight'
                            }
                        },*/
                        data: dataObj.one
                    },
                    {
                        name: '50(含)-55分',
                        type: 'bar',
                        stack: '总量',
                        /*label: {
                            normal: {
                                show: true,
                                position: 'insideRight'
                            }
                        },*/
                        data: dataObj.two,
                    },
                    {
                        name: '55(含)-60分',
                        type: 'bar',
                        stack: '总量',
                       /* label: {
                            normal: {
                                show: true,
                                position: 'insideRight'
                            }
                        },*/
                        data: dataObj.three
                    },
                    {
                        name: '60(含)-65分',
                        type: 'bar',
                        stack: '总量',
                       /* label: {
                            normal: {
                                show: true,
                                position: 'insideRight'
                            }
                        },*/
                        data: dataObj.four
                    },
                    {
                        name: '65(含)-70分',
                        type: 'bar',
                        stack: '总量',
                       /* label: {
                            normal: {
                                show: true,
                                position: 'insideRight'
                            }
                        },*/
                        data: dataObj.five
                    },
                    {
                        name: '70(含)-75分',
                        type: 'bar',
                        stack: '总量',
                        /*label: {
                            normal: {
                                show: true,
                                position: 'insideRight'
                            }
                        },*/
                        data: dataObj.six
                    },
                    {
                        name: '75(含)-80分',
                        type: 'bar',
                        stack: '总量',
                       /* label: {
                            normal: {
                                show: true,
                                position: 'insideRight'
                            }
                        },*/
                        data: dataObj.seven
                    },
                    {
                        name: '80(含)-85分',
                        type: 'bar',
                        stack: '总量',
                        /*label: {
                            normal: {
                                show: true,
                                position: 'insideRight'
                            }
                        },*/
                        data: dataObj.eight
                    },
                    {
                        name: '85(含)以上',
                        type: 'bar',
                        stack: '总量',
                       /* label: {
                            normal: {
                                show: true,
                                position: 'insideRight'
                            }
                        },*/
                        data: dataObj.nine
                    },
                    {
                        name: '其它',
                        type: 'bar',
                        stack: '总量',
                       /* label: {
                            normal: {
                                show: true,
                                position: 'insideRight'
                            }
                        },*/
                        data: dataObj.other
                    }
                ]
         };  

 

 

后台拿到的数据,如下:

 

[{gsId=8022, gsName=西北分公司, type=0, yybcount=4}, 
{gsId=8016, gsName=扬州分公司, type=0, yybcount=4}, 
{gsId=8008, gsName=南京分公司, type=0, yybcount=14409}, 
{gsId=8008, gsName=南京分公司, type=9, yybcount=107}, 
{gsId=8008, gsName=南京分公司, type=1, yybcount=45}, 
{gsId=8008, gsName=南京分公司, type=8, yybcount=8}, 
{gsId=8008, gsName=南京分公司, type=7, yybcount=5}, 
{gsId=8008, gsName=南京分公司, type=6, yybcount=4}, 
{gsId=8008, gsName=南京分公司, type=3, yybcount=3}, 
{gsId=8008, gsName=南京分公司, type=5, yybcount=1}, 
{gsId=8008, gsName=南京分公司, type=2, yybcount=1}, 
{gsId=8008, gsName=南京分公司, type=4, yybcount=1}, 
{gsId=8002, gsName=苏州分公司, type=0, yybcount=5}, 
{gsId=8001, gsName=上海分公司, type=0, yybcount=4}, 
{gsId=8000, gsName=总部特殊业务管理, type=0, yybcount=38616}, 
{gsId=8000, gsName=总部特殊业务管理, type=9, yybcount=133}, 
{gsId=8000, gsName=总部特殊业务管理, type=1, yybcount=72}, 
{gsId=8000, gsName=总部特殊业务管理, type=8, yybcount=46}, 
{gsId=8000, gsName=总部特殊业务管理, type=3, yybcount=42}, 
{gsId=8000, gsName=总部特殊业务管理, type=7, yybcount=40}, 
{gsId=8000, gsName=总部特殊业务管理, type=6, yybcount=32}, 
{gsId=8000, gsName=总部特殊业务管理, type=5, yybcount=28}, 
{gsId=8000, gsName=总部特殊业务管理, type=2, yybcount=15}, 
{gsId=8000, gsName=总部特殊业务管理, type=4, yybcount=2}, 
{gsId=1, gsName=华泰证券, type=0, yybcount=36}]

 

 

需要转换组装的格式,如下:

 

{
gsId=[8000, 8008, 1, 8002, 8022, 8016, 8001], 
gsName=[总部特殊业务管理, 南京分公司, 华泰证券, 苏州分公司, 西北分公司, 扬州分公司, 上海分公司], 
flag=[1, 1, 1, 1, 1, 1, 1], 
other=[38616, 14409, 36, 5, 4, 4, 4]
one=[72, 45, 0, 0, 0, 0, 0], 
two=[15, 1, 0, 0, 0, 0, 0], 
three=[42, 3, 0, 0, 0, 0, 0], 
four=[2, 1, 0, 0, 0, 0, 0], 
five=[28, 1, 0, 0, 0, 0, 0],
six=[32, 4, 0, 0, 0, 0, 0],
seven=[40, 5, 0, 0, 0, 0, 0], 
eight=[46, 8, 0, 0, 0, 0, 0], 
nine=[133, 107, 0, 0, 0, 0, 0]
}

 

思路分析:

List<type1> type1
List<type2>
...


for(1-10){
  object obj = list.get(gsId.get(i),1);//循环9个固定阀值,当gsId,类型相等则返回该对象,否则返回null

  if(null !=obj){

    type1.add(obj.getYybcount());
  }else{ 
    type1.add(
"0");
  }
}

 

公司名称              50分以下           50(含)-55分      55(含)-60分    60(含)-65分     65(含)-70分     70(含)-75分     75(含)-80分     80(含)-85分      85(含)以上        其它
华泰证券                  0                 0                 0                0                0              0                 0                 0              0               36
总部特殊业务管理           72                15                42               2                28             32                40                46            133              38616
上海分公司                0                 0                 0                0                0              0                 0                 0              0                4
苏州分公司                0                 0                 0                0                0              0                 0                 0              0                5
南京分公司                45                1                 3                1                1              4                 5                 8             107              14409
扬州分公司                0                 0                 0                0                0              0                 0                 0              0                4
西北分公司                0                 0                 0                0                0              0                 0                 0              0                4   

 

实现代码,如下:

    @SuppressWarnings("unchecked")
    public static  Map<String, Object> assemReturnMap(Map<String, Object> returnMap, List<Map<String, String>> list,String flag) {
        
        List<Map<String, String>> lists = mergeList(list);
        ComparatorMap comparator=new ComparatorMap();
        Collections.sort(lists, comparator);
        
        //组装name集合
        List<String> gsNameList = new ArrayList<String>();
        List<String> gsIdList = new ArrayList<String>();
        List<String> flags = new ArrayList<String>();
        for(Map<String,String> mp:lists){
            String gsName = mp.get("gsName");
            if(!gsNameList.contains(gsName)){
                gsNameList.add(gsName);
            }
            String gsId = String.valueOf(mp.get("gsId"));
            if(!gsIdList.contains(gsId)){
                gsIdList.add(gsId);     
                if("0".equals(flag)){
                    flags.add("0");
                }
                if("1".equals(flag)){
                    flags.add("1");
                }
            }
        }
        List<String> yybcountListOther = new ArrayList<String>();
        List<String> yybcountListOne = new ArrayList<String>();
        List<String> yybcountListTwo = new ArrayList<String>();
        List<String> yybcountListThree = new ArrayList<String>();
        List<String> yybcountListFour = new ArrayList<String>();
        List<String> yybcountListFive = new ArrayList<String>();
        List<String> yybcountListSix = new ArrayList<String>();
        List<String> yybcountListSeven = new ArrayList<String>();
        List<String> yybcountListEight = new ArrayList<String>();
        List<String> yybcountListNine = new ArrayList<String>();
        
        for (int i = 0; i < 10; i++) {
            for (String gsId : gsIdList) {
                Map<String, String> obj = getGsInfoByGsIdAndType(list, gsId, i);
                if (i == 0) {
                    if (null != obj) {
                        yybcountListOther.add(null != obj.get("yybcount") ? String.valueOf(obj.get("yybcount")) : "0");
                    } else {
                        yybcountListOther.add("0");
                    }
                }
                if (i == 1) {
                    if (null != obj) {
                        yybcountListOne.add(null != obj.get("yybcount") ? String.valueOf(obj.get("yybcount")) : "0");
                    } else {
                        yybcountListOne.add("0");
                    }
                }
                if (i == 2) {
                    if (null != obj) {
                        yybcountListTwo.add(null != obj.get("yybcount") ? String.valueOf(obj.get("yybcount")) : "0");
                    } else {
                        yybcountListTwo.add("0");
                    }
                }
                if (i == 3) {
                    if (null != obj) {
                        yybcountListThree.add(null != obj.get("yybcount") ? String.valueOf(obj.get("yybcount")) : "0");
                    } else {
                        yybcountListThree.add("0");
                    }
                }
                if (i == 4) {
                    if (null != obj) {
                        yybcountListFour.add(null != obj.get("yybcount") ? String.valueOf(obj.get("yybcount")) : "0");
                    } else {
                        yybcountListFour.add("0");
                    }
                }
                if (i == 5) {
                    if (null != obj) {
                        yybcountListFive.add(null != obj.get("yybcount") ? String.valueOf(obj.get("yybcount")) : "0");
                    } else {
                        yybcountListFive.add("0");
                    }
                }
                if (i == 6) {
                    if (null != obj) {
                        yybcountListSix.add(null != obj.get("yybcount") ? String.valueOf(obj.get("yybcount")) : "0");
                    } else {
                        yybcountListSix.add("0");
                    }
                }
                if (i == 7) {
                    if (null != obj) {
                        yybcountListSeven.add(null != obj.get("yybcount") ? String.valueOf(obj.get("yybcount")) : "0");
                    } else {
                        yybcountListSeven.add("0");
                    }
                }
                if (i == 8) {
                    if (null != obj) {
                        yybcountListEight.add(null != obj.get("yybcount") ? String.valueOf(obj.get("yybcount")) : "0");
                    } else {
                        yybcountListEight.add("0");
                    }
                }
                if (i == 9) {
                    if (null != obj) {
                        yybcountListNine.add(null != obj.get("yybcount") ? String.valueOf(obj.get("yybcount")) : "0");
                    } else {
                        yybcountListNine.add("0");
                    }
                }
            }
        }
        returnMap.put("gsId", gsIdList);
        returnMap.put("gsName", gsNameList);
        returnMap.put("flag", flags);
        returnMap.put("one", yybcountListOne);
        returnMap.put("two", yybcountListTwo);
        returnMap.put("three", yybcountListThree);
        returnMap.put("four", yybcountListFour);
        returnMap.put("five", yybcountListFive);
        returnMap.put("six", yybcountListSix);
        returnMap.put("seven", yybcountListSeven);
        returnMap.put("eight", yybcountListEight);
        returnMap.put("nine", yybcountListNine);  
        returnMap.put("other", yybcountListOther);
        returnMap.put("list", list);
        return returnMap;
    }

 

/**
     * 通过公司ID和类型获取公司信息
     * @param list
     * @param gsId
     * @param type
     * @return
     */
    private static Map<String, String> getGsInfoByGsIdAndType(List<Map<String, String>> list, String gsId, int type) {
        for (Map<String, String> obj : list) {
            if (gsId.equals(String.valueOf(obj.get("gsId"))) && String.valueOf(type).equals(String.valueOf(obj.get("type")))) {
                return obj;
            }
        }
        return null;
    }

 

/**
     * 合并重复数据
     * @param oneList
     * @return
     */
    public static List<Map<String, String>> mergeList(List<Map<String, String>> oneList) {
        List<Map<String,String>> list = new ArrayList<Map<String,String>>(); 
        for (int i = 0; i < oneList.size(); i++) {
            String gsId = String.valueOf(oneList.get(i).get("gsId"));
            int flag = 0;
            for (int j = 0; j < list.size(); j++) {
                String gsId_ = String.valueOf(list.get(j).get("gsId"));
                if (null!=gsId&&null!=gsId_&&!"".equals(gsId)&&!"".equals(gsId_)&&gsId.equals(gsId_)) {
                    int sum = Integer.parseInt(String.valueOf(oneList.get(i).get("yybcount"))) + Integer.parseInt(String.valueOf(list.get(j).get("yybcount")));
                    list.get(j).put("yybcount", sum + "");
                    flag = 1;
                    continue;
                }
            }
            if (flag == 0) {
                list.add(oneList.get(i));
            }
        }
        return list;
    }

 

posted @ 2017-06-12 16:53  佚名787  阅读(4122)  评论(0编辑  收藏  举报