String querySystemcheckGroup = "SELECT T.ASSET_GUID,T.ASSET_ID,T.TYPE_LEVEL,T.TYPE_NAME,T.TYPE_CODE,T.SORT_ID,T.PARENT_ID FROM META_KB_ASSETTYPE T " +
"WHERE T.PARENT_ID IS NULL AND T.DELETE_FLAG=0";
List<Map<Object, Object>> listmap = super.queryData(querySystemcheckGroup, null);
Map<Object, Object> root = listmap.get(0);//查询根节点
Map<Object,Object> rootAttributesArray = new HashMap<Object,Object>();
List<Map<Object, Object>> firstListChild = queryAssetTypeTreeChild((String) root.get("ASSET_GUID"));
for(int i = 0;i< firstListChild.size();i++){//循环1级节点
Map<Object, Object> firstChild = firstListChild.get(i);
Map<Object,Object> firstAttributesArray = new HashMap<Object,Object>();
List<Map<Object, Object>> secondListChild = queryAssetTypeTreeChild((String) firstChild.get("ASSET_GUID"));
for(int j = 0;j< secondListChild.size();j++){//循环2级节点
Map<Object, Object> secondChild = secondListChild.get(j);
Map<Object,Object> secondAttributesArray = new HashMap<Object,Object>();
List<Map<Object, Object>> thirdListChild = queryAssetTypeTreeChild((String) secondChild.get("ASSET_GUID"));
for(int k = 0;k< thirdListChild.size();k++){//循环3级节点
Map<Object, Object> thirdChild = thirdListChild.get(k);
Map<Object,Object> thirdAttributesArray = new HashMap<Object,Object>();
List<Map<Object, Object>> fourthListChild = queryAssetTypeTreeChild((String) thirdChild.get("ASSET_GUID"));
for(int f = 0;f< fourthListChild.size();f++){//循环4级节点
Map<Object, Object> fourthChild = fourthListChild.get(f);
Map<Object,Object> fourthAttributesArray = new HashMap<Object,Object>();
List<Map<Object, Object>> fifthListChild = queryAssetTypeTreeChild((String) fourthChild.get("ASSET_GUID"));
for(int l = 0;l< fifthListChild.size();l++){//底层节点
Map<Object, Object> fifthChild = fifthListChild.get(l);
Map<Object,Object> fifthAttributesArray = new HashMap<Object,Object>();
fifthAttributesArray.putAll(fifthChild);
fifthChild.put("state", "closed");
fifthChild.put("id", fifthAttributesArray.remove("ASSET_GUID")) ;
fifthChild.put("text", fifthAttributesArray.remove("TYPE_NAME"));
fifthChild.put("attributes", fifthAttributesArray);
}
fourthAttributesArray.putAll(fourthChild);
fourthChild.put("children", fifthListChild);
fourthChild.put("state", "open");
fourthChild.put("id", fourthAttributesArray.remove("ASSET_GUID")) ;
fourthChild.put("text", fourthAttributesArray.remove("TYPE_NAME"));
fourthChild.put("attributes", fourthAttributesArray);
}
thirdAttributesArray.putAll(thirdChild);
thirdChild.put("state", "open");
thirdChild.put("children", fourthListChild);
thirdChild.put("id", thirdAttributesArray.remove("ASSET_GUID")) ;
thirdChild.put("text", thirdAttributesArray.remove("TYPE_NAME"));
thirdChild.put("attributes", thirdAttributesArray);
}
secondAttributesArray.putAll(secondChild);
secondChild.put("state", "open");
secondChild.put("children", thirdListChild);
secondChild.put("id", secondAttributesArray.remove("ASSET_GUID")) ;
secondChild.put("text", secondAttributesArray.remove("TYPE_NAME"));
secondChild.put("attributes", secondAttributesArray);
}
firstAttributesArray.putAll(firstChild);
firstChild.put("state", "open");
firstChild.put("children", secondListChild);
firstChild.put("id", firstAttributesArray.remove("ASSET_GUID")) ;
firstChild.put("text", firstAttributesArray.remove("TYPE_NAME"));
firstChild.put("attributes", firstAttributesArray);
}
rootAttributesArray.putAll(root);
root.put("children", firstListChild);
root.put("state", "open");
root.put("id", rootAttributesArray.remove("ASSET_GUID")) ;
root.put("text", rootAttributesArray.remove("TYPE_NAME"));
root.put("attributes", rootAttributesArray);
return listmap;
不知道代码排版如何,就是想问下,这样如何实现for递归
6 个解决方案
#1

#2
可以啊,把递归算法封装在一个方法里,然后对这个方法直接调用。
注意递归的跳出条件。
注意递归的跳出条件。
#3


protected List<Map<Object, Object>> queryChild(List<Map<Object, Object>> listmap) throws Exception{
for(int l = 0;l< listmap.size();l++){
Map<Object, Object> root = listmap.get(l);
Map<Object,Object> attributesArray = new HashMap<Object,Object>();
List<Map<Object, Object>> listChild = queryAssetTypeTreeChild((String) root.get("ASSET_GUID"));
attributesArray.putAll(root);
if(root.get("TYPE_LEVEL") == "4"){
root.put("state", "closed");
}else{
root.put("state", "open");
}
root.put("children", queryChild(listChild));
root.put("id", attributesArray.remove("ASSET_GUID")) ;
root.put("text", attributesArray.remove("TYPE_NAME"));
root.put("attributes", attributesArray);
}
return listmap;
}
#4

#5
你这个五层的循环代码基本一致,刚好符合递归的条件。
#6
除了一个小地方不同之外,其他一致

#1

#2
可以啊,把递归算法封装在一个方法里,然后对这个方法直接调用。
注意递归的跳出条件。
注意递归的跳出条件。
#3


protected List<Map<Object, Object>> queryChild(List<Map<Object, Object>> listmap) throws Exception{
for(int l = 0;l< listmap.size();l++){
Map<Object, Object> root = listmap.get(l);
Map<Object,Object> attributesArray = new HashMap<Object,Object>();
List<Map<Object, Object>> listChild = queryAssetTypeTreeChild((String) root.get("ASSET_GUID"));
attributesArray.putAll(root);
if(root.get("TYPE_LEVEL") == "4"){
root.put("state", "closed");
}else{
root.put("state", "open");
}
root.put("children", queryChild(listChild));
root.put("id", attributesArray.remove("ASSET_GUID")) ;
root.put("text", attributesArray.remove("TYPE_NAME"));
root.put("attributes", attributesArray);
}
return listmap;
}
#4

#5
你这个五层的循环代码基本一致,刚好符合递归的条件。
#6
除了一个小地方不同之外,其他一致
