Tree的实现

本文介绍了一种构建三级下拉Tree的方法,通过遍历数据库获取一、二、三级节点,并利用Map进行父子节点间的关联匹配,最终实现了一个完整的三级Tree结构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

系统中有一个三级下拉的Tree
下拉最多三级
数据表信息
这里写图片描述
一级的PID,为空
二级的PID,为一级的ID
三级的PID,为二级的ID

返回所有

@Override
public List<AssayItemClassExt> selectAllList() {
    List<AssayItemClassExt> list = assayItemClassExtMapper.selectAllList();
    // 一级、二级、三级
    List<AssayItemClassExt> listOne = new ArrayList<>();
    List<AssayItemClassExt> listTwo = new ArrayList<>();
    List<AssayItemClassExt> listThree = new ArrayList<>();
    for (int i = 0; i < list.size(); i++) {
        AssayItemClassExt assayItemClassExt = list.get(i);
        Short index = assayItemClassExt.getTier();
        if (index == 1) {
            listOne.add(assayItemClassExt);
        }
        if (index == 2) {
            listTwo.add(assayItemClassExt);
        }
        if (index == 3) {
            listThree.add(assayItemClassExt);
        }
    }

    // 一级PID
    Map<String, List<AssayItemClassExt>> parentIdOne = new HashMap<>();
    for (int i = 0; i < listOne.size(); i++) {
        String key = listOne.get(i).getId();
        List<AssayItemClassExt> parentIdOneList = new ArrayList<>();
        parentIdOne.put(key, parentIdOneList);
    }
    // 过滤二级节点
    for (Map.Entry<String, List<AssayItemClassExt>> entry : parentIdOne.entrySet()) {
        String key = entry.getKey();
        List<AssayItemClassExt> value = entry.getValue();
        for (int i = 0; i < listTwo.size(); i++) {
            AssayItemClassExt entity = listTwo.get(i);
            if (key.equals(entity.getPid())) {
                value.add(entity);
            }
        }
    }

    // 二级PID
    Map<String, List<AssayItemClassExt>> parentIdTwo = new HashMap<>();
    for (int i = 0; i < listTwo.size(); i++) {
        String key = listTwo.get(i).getId();
        List<AssayItemClassExt> parentIdTwoList = new ArrayList<>();
        parentIdTwo.put(key, parentIdTwoList);
    }
    // 过滤三级节点
    for (Map.Entry<String, List<AssayItemClassExt>> entry : parentIdTwo.entrySet()) {
        String key = entry.getKey();
        List<AssayItemClassExt> value = entry.getValue();
        for (int i = 0; i < listThree.size(); i++) {
            AssayItemClassExt entity = listThree.get(i);
            if (key.equals(entity.getPid())) {
                value.add(entity);
            }
        }
    }

    // 清空list
    list.clear();
    // 重组list
    for (int i = 0; i < listOne.size(); i++) {
        AssayItemClassExt entityOne = listOne.get(i);
        list.add(entityOne);
        String keyOne = entityOne.getId();
        List<AssayItemClassExt> valueOne = parentIdOne.get(keyOne);
        if (valueOne.size() > 0) {
            for (AssayItemClassExt entityTwo : valueOne) {
                list.add(entityTwo);
                String keyTwo = entityTwo.getId();
                List<AssayItemClassExt> valueTwo = parentIdTwo.get(keyTwo);
                if (valueTwo.size() > 0) {
                    list.addAll(valueTwo);
                }
            }
        }
    }
    return list;
}

根据三级ids,返回所有

@Override
public List<AssayItemClassExt> selectListByIds(String[] ids){
    if (ids != null && !ids.equals("")) {
        List<AssayItemClassExt> list = assayItemClassExtMapper.selectAllList();
        // 一级、二级、三级
        List<AssayItemClassExt> listOne = new ArrayList<>();
        List<AssayItemClassExt> listTwo = new ArrayList<>();
        List<AssayItemClassExt> listThree = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            AssayItemClassExt assayItemClassExt = list.get(i);
            Short index = assayItemClassExt.getTier();
            if (index == 1) {
                listOne.add(assayItemClassExt);
            }
            if (index == 2) {
                listTwo.add(assayItemClassExt);
            }
            if (index == 3) {
                listThree.add(assayItemClassExt);
            }
        }
        List<AssayItemClassExt> listNew=new ArrayList<>();
        for(String id:ids){
            for(AssayItemClassExt assayItemClassExt:listThree){
                if(id.equals(assayItemClassExt.getId())){
                    listNew.add(assayItemClassExt);
                }
            }
        }
        listThree.clear();
        listThree.addAll(listNew);
        // 二级
        Map<String, AssayItemClassExt> mapTwo = new HashMap<>();
        for (int i = 0; i < listThree.size(); i++) {
            String key = listThree.get(i).getPid();
            if (!mapTwo.containsKey(key)) {
                AssayItemClassExt assayItemClassExt = null;
                mapTwo.put(key, assayItemClassExt);
            }
        }
        for (Map.Entry<String, AssayItemClassExt> entry : mapTwo.entrySet()) {
            String key = entry.getKey();
            for (int i = 0; i < listTwo.size(); i++) {
                if (key.equals(listTwo.get(i).getId())) {
                    mapTwo.put(key, listTwo.get(i));
                }
            }
        }
        listTwo.clear();
        for (AssayItemClassExt value : mapTwo.values()) {
            listTwo.add(value);
        }
        // 一级
        Map<String, AssayItemClassExt> mapOne = new HashMap<>();
        for (Map.Entry<String, AssayItemClassExt> entry : mapTwo.entrySet()) {
            AssayItemClassExt value = entry.getValue();
            String key = value.getPid();
            if (!mapOne.containsKey(key)) {
                AssayItemClassExt assayItemClassExt = null;
                mapOne.put(key, assayItemClassExt);
            }
        }
        for (Map.Entry<String, AssayItemClassExt> entry : mapOne.entrySet()) {
            String key = entry.getKey();
            for (int i = 0; i < listOne.size(); i++) {
                if (key.equals(listOne.get(i).getId())) {
                    mapOne.put(key, listOne.get(i));
                }
            }
        }
        listOne.clear();
        for (AssayItemClassExt value : mapOne.values()) {
            listOne.add(value);
        }
        // 清空list
        list.clear();
        // 重组list
        for (AssayItemClassExt entityOne : listOne) {
            list.add(entityOne);
            String idOne = entityOne.getId();
            for (AssayItemClassExt entityTwo : listTwo) {
                if (idOne.equals(entityTwo.getPid())) {
                    list.add(entityTwo);
                    String idTwo = entityTwo.getId();
                    for (AssayItemClassExt entityThree : listThree) {
                        if (idTwo.equals(entityThree.getPid())) {
                            list.add(entityThree);
                        }
                    }
                }
            }
        }
        return list;
    }
    return null;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值