个人笔记-递归构造树(从数据库中一次查询具有树结构的数据,返回到前端)

 
/**
     * 根据类型列表构建树结构
     *
     * @param rootId
     * @param typeList
     * @return
     */
    public static Map<String, Object> buildTypeTree(String rootId, List<Map<String, Object>> typeList) {
        Map<String, Object> treeMap = new HashMap<String, Object>();
        treeMap.put("ID", rootId);
        for (Map<String, Object> m : typeList) {
            String idPath = (String) m.get("IDPATH");
            if (idPath == null) {
                treeMap.put("ID", m.get("ID"));
                treeMap.put("NAME", m.get("NAME"));
                continue;
            }
            String[] ids = idPath.split("->");
            m.remove("IDPATH");
            if (ids.length > 0)
                createTree(treeMap, ids, 0, m);
        }
        return treeMap;
    }

    public static void createTree(Map<String, Object> treeMap, String[] ids, int index, Map<String, Object> nodeMap) {
        List<Map<String, Object>> childList = (List<Map<String, Object>>) treeMap.get("CHILD");
        if (childList == null) {
            treeMap.put("CHILD", new ArrayList<Map<String, Object>>());
            childList = (List<Map<String, Object>>) treeMap.get("CHILD");
        }
        if (childList.size() == 0) {
            if (index < ids.length) {
                if (ids.length - index == 1) {
                    childList.add(nodeMap);
                } else {
                    Map<String, Object> childMap = new HashMap<String, Object>();
                    childMap.put("ID", ids[index]);
                    childList.add(childMap);
                    createTree(childMap, ids, ++index, nodeMap);
                }
            }
        } else {
            boolean add = true;
            for (Map<String, Object> childMap : childList) {
                if (childMap.get("ID").equals(ids[index])) {
                    add = false;
                    if (ids.length - index == 1) {
                        childMap.putAll(nodeMap);
                    } else {
                        createTree(childMap, ids, ++index, nodeMap);
                    }
                }
            }
            if (add) {
                if (ids.length - index == 1) {
                    childList.add(nodeMap);
                } else {
                    Map<String, Object> childMap = new HashMap<String, Object>();
                    childMap.put("ID", ids[index]);
                    childList.add(childMap);
                    createTree(childMap, ids, ++index, nodeMap);
                }
            }
        }
    }


 

/**
	 * 根据code对事件排序
	 * @param child
	 */
	private void sort(List<Map<String, Object>> child) {
		Collections.sort(child, new Comparator<Map<String, Object>>() {
			public int compare(Map<String, Object> o1, Map<String, Object> o2) {
				String code1 = (String) o1.get(("CODE"));
				String code2 = (String) o2.get(("CODE"));
				if (code1 == null && code2 == null) {
					return 0;
				} else if (code1 != null && code2 == null) {
					return 1;
				} else if (code1 == null && code2 != null) {
					return -1;
				} else if (code1 != null && code2 != null) {
					return code1.compareToIgnoreCase(code2);
				}
				return -1;
			}
		});
		List<Map<String, Object>> $mapList1 = new ArrayList<>();
		List<Map<String, Object>> $mapList2 = new ArrayList<>();
		for (int i = 0, j = child.size(); i < j; i++) {
			String code = (String) child.get(i).get("CODE");
			if (code == null) {
				$mapList2.add($mapList1.size(), child.get(i));
			} else {
				$mapList1.add(child.get(i));
			}
		}
		$mapList1.addAll($mapList2);
	}

public static void sort(Map<String, Object> treeMap) {
        List<Map<String, Object>> child = (List<Map<String, Object>>) treeMap.get("CHILD");
        if (child != null) {
            sort(child);
            for (Map<String, Object> map : child) {
                sort(map);
            }
        }
    }




String sql="select substr(sys_connect_by_path(parent_id,'->'),3) idpath,id,name from sc_t_affair_type start with parent_id='00001' connect by prior id=parent_id";






public static void main(String ar[]) throws Exception{
        String[] ids1 = {"1","2","3","4"};
        String[] ids11 = {"1","2","3","5"};
        String[] ids2 = {"1","22"};
        String[] ids22 = {"1","222"};
        String[][] ids={ids1,ids11,ids2,ids22};
        Map<String,Object> treeMap=new HashMap<String,Object>();
        treeMap.put("CHILD",new ArrayList<Map<String,Object>>());
        treeMap.put("ID","1");
        for(int i=0;i<ids.length;i++){
            Map<String,String> nodeMap=new HashMap<>();
            nodeMap.put("NAME","name"+i);
            nodeMap.put("ID", ids[i][ids[i].length - 1]);
            createTree(treeMap, ids[i], 1, nodeMap);
        }
        System.out.println(treeMap);
    }

    public static void createTree(Map<String,Object> treeMap,String[] ids,int index, Map<String,String> nodeMap){

        List<Map<String,Object>> childList=(List<Map<String,Object>>)treeMap.get("CHILD");
        if(childList==null){
            treeMap.put("CHILD",new ArrayList<Map<String,Object>>());
            childList=(List<Map<String,Object>>)treeMap.get("CHILD");
        }
        if(childList.size()==0){
            Map<String,Object> childMap=new HashMap<String,Object>();
            childMap.put("ID",ids[index++]);
            childList.add(childMap);
            System.out.println(ids.length);
            if(index<ids.length)
                createTree(childMap,ids,index,nodeMap);
        }else{
            boolean add=true;
            for(Map<String,Object> map:childList){
                if(ids[index].equals(map.get("ID"))){
                    add=false;
                    createTree(map,ids,++index,nodeMap);
                }
            }
            if(add){
                Map<String,Object> childMap=new HashMap<String,Object>();
                childMap.put("ID",ids[index++]);
                childList.add(childMap);
                if(index<ids.length)
                    createTree(childMap,ids,index,nodeMap);
            }
        }
    }



                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值