递归实现区划树形结构显示
在开发项目过程中,经常会出现显示一些树形结构数据的需求,下面使用递归处理数据,构建树形数据结构。
package com.offer.interview.leetcode;
import java.io.Serializable;
import java.util.List;
public class RegionOrgVo implements Serializable {
private String code;
private String name;
private String regType;
private String parentCode;
private List<RegionOrgVo> children;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRegType() {
return regType;
}
public void setRegType(String regType) {
this.regType = regType;
}
public String getParentCode() {
return parentCode;
}
public void setParentCode(String parentCode) {
this.parentCode = parentCode;
}
public List<RegionOrgVo> getChildren() {
return children;
}
public void setChildren(List<RegionOrgVo> children) {
this.children = children;
}
}
package com.offer.interview.leetcode;
import com.alibaba.fastjson.JSON;
import java.util.ArrayList;
import java.util.List;
public class RegionOrgVoMain {
private static RegionOrgVo getTreeChildren(RegionOrgVo vo,List<RegionOrgVo> list){
List<RegionOrgVo> childList = new ArrayList<>();
for(RegionOrgVo rov : list){
if(vo.getCode().equals(rov.getParentCode())){
childList.add(getTreeChildren(rov,list));
}
}
vo.setChildren(childList);
return vo;
}
public static void main(String[] args) {
RegionOrgVo vo = new RegionOrgVo();
vo.setCode("530000");
vo.setName("云南省");
vo.setParentCode("-1");
vo.setRegType("1");
RegionOrgVo vo1 = new RegionOrgVo();
vo1.setCode("530100");
vo1.setName("昆明市");
vo1.setParentCode("530000");
vo1.setRegType("2");
RegionOrgVo vo11 = new RegionOrgVo();
vo11.setCode("530102");
vo11.setName("五华区");
vo11.setParentCode("530100");
vo11.setRegType("3");
RegionOrgVo vo12 = new RegionOrgVo();
vo12.setCode("530111");
vo12.setName("官渡区");
vo12.setParentCode("530100");
vo12.setRegType("3");
RegionOrgVo vo2 = new RegionOrgVo();
vo2.setCode("530600");
vo2.setName("昭通市");
vo2.setParentCode("530000");
vo2.setRegType("2");
RegionOrgVo vo21 = new RegionOrgVo();
vo21.setCode("530602");
vo21.setName("昭阳区");
vo21.setParentCode("530600");
vo21.setRegType("3");
RegionOrgVo vo22 = new RegionOrgVo();
vo22.setCode("530621");
vo22.setName("鲁甸县");
vo22.setParentCode("530600");
vo22.setRegType("3");
List<RegionOrgVo> list = new ArrayList<>();
list.add(vo1);
list.add(vo11);
list.add(vo12);
list.add(vo2);
list.add(vo21);
list.add(vo22);
System.out.println(JSON.toJSONString(getTreeChildren(vo,list)));
}
}
{
"children": [
{
"children": [
{
"children": [],
"code": "530102",
"name": "五华区",
"parentCode": "530100",
"regType": "3"
},
{
"children": [],
"code": "530111",
"name": "官渡区",
"parentCode": "530100",
"regType": "3"
}
],
"code": "530100",
"name": "昆明市",
"parentCode": "530000",
"regType": "2"
},
{
"children": [
{
"children": [],
"code": "530602",
"name": "昭阳区",
"parentCode": "530600",
"regType": "3"
},
{
"children": [],
"code": "530621",
"name": "鲁甸县",
"parentCode": "530600",
"regType": "3"
}
],
"code": "530600",
"name": "昭通市",
"parentCode": "530000",
"regType": "2"
}
],
"code": "530000",
"name": "云南省",
"parentCode": "-1",
"regType": "1"
}