将一个List.map根据map中的一个key的值去分类,且可以转成自定义树状结构

一、按照map一个列进行分组,返回map

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
     * ListMap根据map中的一个列做分类形成分组
     * @param list 需要形成树状图的listmap
     * @param key 需要做分类的map的key
     * @return 树状map
     */
    public static Map listMapClassification(List> list, String key) {
        //创建一个空的新map
        Map newMap = new HashMap<>();
        //循环listmap
        for (int s = 0; s> mapList = new ArrayList<>();
            //取出当前list循环下的Map
            Map objectMap = list.get(s);
            //取出map中按key分类好类的值列表
            //第一次循环因为没有插入过值,所以会为空
            Object get = newMap.get((String) objectMap.get(key));
            System.out.println("输出4"+ get);
            //判断分好类的当前类的名字下的值是否为空
            if(get!=null){
                //不为空就将泛型转为list
                mapList=(List>) get;
            }
            //将当前循环的下的那个map插入,分好类的list中
            mapList.add(objectMap);
            //将当前循环插入好的list,重新给分类赋值
            newMap.put((String) objectMap.get(key),mapList);
        }
        return newMap;
    }

使用方法

        //两个参数第一个要转换的list,第二个要分类的列
    EntityUtils.listMapClassification(mapList,"TB_TG_Code")

原始数据

TB_ID    TB_TG_Code    TB_TG_Name    TB_TB_Code    TB_TB_Name    TB_TB_Comment    TB_HospCode    TB_HospName   
2   01  结防所病种   0102    复治肺结核       00000000    XXX医院
1   01  结防所病种   0101    初治肺结核       00000000    XXX医院
3   01  结防所病种   0103    结核性胸膜炎      00000000    XXX医院
4   01  结防所病种   0104    单耐药肺结核      00000000    XXX医院
5   01  结防所病种   0105    多耐药肺结核      00000000    XXX医院
6   02  分类二 0106    耐多药肺结核      00000000    XXX医院
{
    "code": 0,
    "msg": null,
    "data": {
        "01": [
            {
                "S_ID": "casc",
                "TB_TG_Name": "结防所病种",
                "TB_TB_Comment": "",
                "TB_HospCode": "00000000",
                "S_OperLevel": "1",
                "TB_ID": "2",
                "TB_TG_Code": "01",
                "TB_TB_Name": "复治肺结核",
                "TB_HospName": "XXX医院",
                "S_TB_Right": "编辑",
                "S_TB_Code": "0102",
                "S_HospCode": "00000000",
                "S_TB_Name": "复治肺结核·",
                "TB_TB_Code": "0102"
            },
            {
                "TB_ID": "1",
                "TB_TG_Code": "01",
                "TB_TB_Name": "初治肺结核",
                "TB_HospName": "XXX医院",
                "TB_TG_Name": "结防所病种",
                "TB_TB_Comment": "",
                "TB_HospCode": "00000000",
                "TB_TB_Code": "0101"
            },
            {
                "TB_ID": "3",
                "TB_TG_Code": "01",
                "TB_TB_Name": "结核性胸膜炎",
                "TB_HospName": "XXX医院",
                "TB_TG_Name": "结防所病种",
                "TB_HospCode": "00000000",
                "TB_TB_Code": "0103"
            },
        ],
        "02": [
            {
                "TB_ID": "6",
                "TB_TG_Code": "02",
                "TB_TB_Name": "耐多药肺结核",
                "TB_HospName": "XXX医院",
                "TB_TG_Name": "分类二",
                "TB_HospCode": "00000000",
                "TB_TB_Code": "0106"
            }
        ]
    }
}

二、按照map一个列进行分组,形成自定义树,返回树状结构

/**
     * 按照map一个列进行分组,形成自定义树,返回树状结构
     * @param list 需要形成树状图的listmap
     * @param key 需要做分类的map的key
     * @return
     */
    public static List>  listMapClassificationTree(List> list, String key) {
        //创建一个空的新map
        List> newList = new ArrayList<>();
        //创建一个空的新map
        Map newMap = new HashMap<>();
        //循环listmap
        for (int s = 0; s> mapList = new ArrayList<>();
            //取出当前list循环下的Map
            Map objectMap = list.get(s);
            //取出map中按key分类好类的值列表
            //第一次循环因为没有插入过值,所以会为空
            Object get = newMap.get((String) objectMap.get(key));
            System.out.println("输出4"+ get);
            //判断分好类的当前类的名字下的值是否为空
            if(get!=null){
                //不为空就将泛型转为list
                mapList=(List>) get;
            }
            //将当前循环的下的那个map插入,分好类的list中
            mapList.add(objectMap);
            //将当前循环插入好的list,重新给分类赋值
            newMap.put((String) objectMap.get(key),mapList);
        }
        //判断map是否为空
        //因为如果为空for-each循环将抛出NullPointerException,因此在遍历前你总是应该检查空引用
        if(newMap!=null){
            Integer i=0;
            //循环这个map将这个map插入list
            for (Map.Entry entry : newMap.entrySet()) {
                //创建临时的map
                Map newMapaddList = new HashMap<>();
//          System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
                newMapaddList.put("child",entry.getValue());
                newMapaddList.put("id",i);
                newMapaddList.put("name",entry.getKey());
                //将临时数据插入要返回的list中
                newList.add(newMapaddList);
                i++;
            }
        }

        return newList;
    }

用法和上面的方法一样

结果

{
    "code": 0,
    "msg": null,
    "data": [
        {
            "name": "分类二",
            "id": 0,
            "child": [
                {
                    "TB_ID": "6",
                    "TB_TG_Code": "02",
                    "TB_TB_Name": "耐多药肺结核",
                    "TB_HospName": "XXX医院",
                    "TB_TG_Name": "分类二",
                    "TB_HospCode": "00000000",
                    "TB_TB_Code": "0106"
                }
            ]
        },
        {
            "name": "结防所病种",
            "id": 1,
            "child": [
                {
                    "S_ID": "ASAS",
                    "TB_TG_Name": "结防所病种",
                    "TB_TB_Comment": "",
                    "TB_HospCode": "00000000",
                    "S_OperLevel": "1",
                    "TB_ID": "1",
                    "TB_TG_Code": "01",
                    "TB_TB_Name": "初治肺结核",
                    "TB_HospName": "XXX医院",
                    "S_TB_Right": "编辑",
                    "S_TB_Code": "0101",
                    "S_HospCode": "00000000",
                    "S_TB_Name": "初治肺结核",
                    "TB_TB_Code": "0101"
                },
                {
                    "S_ID": "casc",
                    "TB_TG_Name": "结防所病种",
                    "TB_TB_Comment": "",
                    "TB_HospCode": "00000000",
                    "S_OperLevel": "1",
                    "TB_ID": "2",
                    "TB_TG_Code": "01",
                    "TB_TB_Name": "复治肺结核",
                    "TB_HospName": "XXX医院",
                    "S_TB_Right": "编辑",
                    "S_TB_Code": "0102",
                    "S_HospCode": "00000000",
                    "S_TB_Name": "复治肺结核·",
                    "TB_TB_Code": "0102"
                },
                {
                    "TB_ID": "3",
                    "TB_TG_Code": "01",
                    "TB_TB_Name": "结核性胸膜炎",
                    "TB_HospName": "XXX医院",
                    "TB_TG_Name": "结防所病种",
                    "TB_HospCode": "00000000",
                    "TB_TB_Code": "0103"
                },
                {
                    "TB_ID": "4",
                    "TB_TG_Code": "01",
                    "TB_TB_Name": "单耐药肺结核",
                    "TB_HospName": "XXX医院",
                    "TB_TG_Name": "结防所病种",
                    "TB_HospCode": "00000000",
                    "TB_TB_Code": "0104"
                },
                {
                    "TB_ID": "5",
                    "TB_TG_Code": "01",
                    "TB_TB_Name": "多耐药肺结核",
                    "TB_HospName": "XXX医院",
                    "TB_TG_Name": "结防所病种",
                    "TB_HospCode": "00000000",
                    "TB_TB_Code": "0105"
                }
            ]
        }
    ]
}

你可能感兴趣的:(将一个List.map根据map中的一个key的值去分类,且可以转成自定义树状结构)