你知道的越多,你不知道的越多
点赞再看,养成习惯
如果您有疑问或者见解,或者没有积分想获取项目和定制项目,欢迎指教:
企鹅:869192208文章来源:https://www.toymoban.com/news/detail-529883.html
需求
将一个包含了父子关系的单位对象的 List 数据,转成一个带层级关系的数据。文章来源地址https://www.toymoban.com/news/detail-529883.html
原始数据,用 json 格式展示
[
{
"areaName": "广东省",
"areaSeq": "BQ6Ig2ewmu51ja1XlUQ",
"credNum": "13440000455857174Y",
"delFlag": "0",
"type": "1",
"unitCode": "GEqONvGif1pkgeypVsW",
"unitName": "广东省残疾人联合会"
},
{
"areaName": "广州市",
"areaSeq": "nbaLYQdhotXvAs2QXaW",
"delFlag": "0",
"parentUnitCode": "27DYBhqxPhWkxyyShIZ",
"type": "2",
"unitCode": "5coZCC2viVbTAotmTIW",
"unitName": "测试部门"
},
{
"areaName": "广州市",
"areaSeq": "nbaLYQdhotXvAs2QXaW",
"credNum": "114400006715714882",
"delFlag": "0",
"type": "1",
"unitCode": "27DYBhqxPhWkxyyShIZ",
"unitName": "测试"
},
{
"areaName": "广州市",
"areaSeq": "nbaLYQdhotXvAs2QXaW",
"delFlag": "0",
"parentUnitCode": "1yfUGoRc0EVyejByf9a",
"type": "2",
"unitCode": "iaqpYx0lUKzrum8oYMl",
"unitName": "事项测试部门43"
},
{
"areaName": "广州市",
"areaSeq": "nbaLYQdhotXvAs2QXaW",
"credNum": "11440000006941354C",
"delFlag": "0",
"parentUnitCode": "8wUPbECBVx75UqQ6gjM",
"type": "1",
"unitCode": "1yfUGoRc0EVyejByf9a",
"unitName": "市场处"
},
{
"areaName": "广州市",
"areaSeq": "nbaLYQdhotXvAs2QXaW",
"credNum": "11440000553612461J",
"delFlag": "0",
"parentUnitCode": "8wUPbECBVx75UqQ6gjM",
"type": "1",
"unitCode": "6DZWgfIUzHa0wk8ixKz",
"unitName": "广东省人力资源和社会保障厅"
},
{
"areaName": "中国",
"areaSeq": "VkThH0HKYsm6uLB1vSU",
"delFlag": "0",
"parentUnitCode": "N0mcwIHkncfZJG3jj0E",
"type": "2",
"unitCode": "k2OHsF7ZnpYADkJbiGi",
"unitName": "教育厅-1部门"
},
{
"areaName": "中国",
"areaSeq": "VkThH0HKYsm6uLB1vSU",
"delFlag": "0",
"parentUnitCode": "N0mcwIHkncfZJG3jj0E",
"type": "1",
"unitCode": "TLgo7tY9HbD9TMZqRxL",
"unitName": "教育厅-1单位"
},
{
"areaName": "中国",
"areaSeq": "VkThH0HKYsm6uLB1vSU",
"credNum": "11440000006940116U",
"delFlag": "0",
"type": "1",
"unitCode": "N0mcwIHkncfZJG3jj0E",
"unitName": "广东省教育厅"
},
{
"areaName": "广州市",
"areaSeq": "nbaLYQdhotXvAs2QXaW",
"delFlag": "0",
"parentUnitCode": "pylto8i5gezxWPsLulf",
"type": "2",
"unitCode": "dAVzZSmgFAuBhk05Tmw",
"unitName": "审批科"
},
{
"areaName": "广州市",
"areaSeq": "nbaLYQdhotXvAs2QXaW",
"credNum": "11440100MB2C91891K",
"delFlag": "0",
"type": "1",
"unitCode": "pylto8i5gezxWPsLulf",
"unitName": "广州市市场监督管理局"
},
{
"areaName": "广州市",
"areaSeq": "nbaLYQdhotXvAs2QXaW",
"delFlag": "0",
"parentUnitCode": "6DZWgfIUzHa0wk8ixKz",
"type": "2",
"unitCode": "FMZpUJ3fCtnllZAAXPT",
"unitName": "测试部门-2-1"
},
{
"areaName": "广州市",
"areaSeq": "nbaLYQdhotXvAs2QXaW",
"delFlag": "0",
"parentUnitCode": "8wUPbECBVx75UqQ6gjM",
"type": "2",
"unitCode": "hMAxUNUDfBdlRT6P5NT",
"unitName": "文物局"
},
{
"areaName": "广州市",
"areaSeq": "nbaLYQdhotXvAs2QXaW",
"delFlag": "0",
"parentUnitCode": "xWKRcZWjYTtuy5gMqmq",
"type": "2",
"unitCode": "P0ZjaRLjCaTmk1vCs0t",
"unitName": "事项测试子部门"
},
{
"areaName": "广州市",
"areaSeq": "nbaLYQdhotXvAs2QXaW",
"credNum": "114400000069411352",
"delFlag": "0",
"parentUnitCode": "8wUPbECBVx75UqQ6gjM",
"type": "1",
"unitCode": "xWKRcZWjYTtuy5gMqmq",
"unitName": "审核处"
},
{
"areaName": "广州市",
"areaSeq": "nbaLYQdhotXvAs2QXaW",
"delFlag": "0",
"parentUnitCode": "8wUPbECBVx75UqQ6gjM",
"type": "2",
"unitCode": "PCkQHkCqBQ8G433PlpN",
"unitName": "准入处"
},
{
"areaName": "广州市",
"areaSeq": "nbaLYQdhotXvAs2QXaW",
"credNum": "11440000MB2C87614P",
"delFlag": "0",
"type": "1",
"unitCode": "8wUPbECBVx75UqQ6gjM",
"unitName": "文旅局"
},
{
"areaName": "广州市",
"areaSeq": "nbaLYQdhotXvAs2QXaW",
"delFlag": "0",
"parentUnitCode": "rBzaXFIRKovid7dgbyY",
"type": "2",
"unitCode": "KZlSpr0M9svbTxswtVp",
"unitName": "001部门"
},
{
"areaName": "广州市",
"areaSeq": "nbaLYQdhotXvAs2QXaW",
"credNum": "11440000K30220376L",
"delFlag": "0",
"type": "1",
"unitCode": "rBzaXFIRKovid7dgbyY",
"unitName": "密码局"
},
{
"areaName": "广州市",
"areaSeq": "nbaLYQdhotXvAs2QXaW",
"delFlag": "0",
"parentUnitCode": "t9nq70CPS9PCwrL7ury",
"type": "2",
"unitCode": "IA6mo3clsYao9qRqVKB",
"unitName": "测试单位4-部门2"
},
{
"areaName": "广州市",
"areaSeq": "nbaLYQdhotXvAs2QXaW",
"delFlag": "0",
"parentUnitCode": "z5N2zSetiup81QAJzok",
"type": "2",
"unitCode": "PqIu1vdrPEMcgw9QLil",
"unitName": "测试单位-部门2"
},
{
"areaName": "中国",
"areaSeq": "VkThH0HKYsm6uLB1vSU",
"delFlag": "0",
"parentUnitCode": "yK78GEa2MyLoTsv7cl8",
"type": "2",
"unitCode": "oL5ku79eKjSD3wsonE2",
"unitName": "测试单位3-部门2"
},
{
"areaName": "中国",
"areaSeq": "VkThH0HKYsm6uLB1vSU",
"delFlag": "0",
"parentUnitCode": "yK78GEa2MyLoTsv7cl8",
"type": "2",
"unitCode": "NZ7nrjiR2Ipp4jUvwdg",
"unitName": "测试单位3-部门1"
},
{
"areaName": "广州市",
"areaSeq": "nbaLYQdhotXvAs2QXaW",
"delFlag": "0",
"parentUnitCode": "z5N2zSetiup81QAJzok",
"type": "2",
"unitCode": "IyfejZ9DXBX24OararK",
"unitName": "测试单位-部门1"
},
{
"areaName": "广州市",
"areaSeq": "nbaLYQdhotXvAs2QXaW",
"delFlag": "0",
"parentUnitCode": "rdF29TiDg8A6uAhtwy8",
"type": "2",
"unitCode": "iAeyIFab9QYfW1qnqIB",
"unitName": "测试单位2-部门2"
},
{
"areaName": "广州市",
"areaSeq": "nbaLYQdhotXvAs2QXaW",
"delFlag": "0",
"parentUnitCode": "rdF29TiDg8A6uAhtwy8",
"type": "2",
"unitCode": "4AE4Eec19qlquekC67b",
"unitName": "测试单位2-部门1"
},
{
"areaName": "广州市",
"areaSeq": "nbaLYQdhotXvAs2QXaW",
"delFlag": "0",
"parentUnitCode": "t9nq70CPS9PCwrL7ury",
"type": "2",
"unitCode": "wBaWrcxKXAORTsr7PXr",
"unitName": "测试单位4-部门1"
},
{
"areaName": "广州市",
"areaSeq": "nbaLYQdhotXvAs2QXaW",
"delFlag": "0",
"parentUnitCode": "EG3iCBPm7bldC2esW1f",
"type": "2",
"unitCode": "RmCztFLB1Ip4Tl0Lcyk",
"unitName": "hello单位-1001部门"
},
{
"areaName": "广州市",
"areaSeq": "nbaLYQdhotXvAs2QXaW",
"credNum": "11440000006940140C",
"delFlag": "0",
"type": "1",
"unitCode": "EG3iCBPm7bldC2esW1f",
"unitName": "市民政局"
},
{
"areaName": "广州市",
"areaSeq": "nbaLYQdhotXvAs2QXaW",
"delFlag": "0",
"parentUnitCode": "wl4XDGhZg71ntFXT8aL",
"type": "2",
"unitCode": "l29g1aszyjTCLmGgKMU",
"unitName": "one测试部门"
},
{
"areaName": "广州市",
"areaSeq": "nbaLYQdhotXvAs2QXaW",
"credNum": "11440000006939908P",
"delFlag": "0",
"type": "1",
"unitCode": "wl4XDGhZg71ntFXT8aL",
"unitName": "市医保局"
},
{
"areaName": "广州市",
"areaSeq": "nbaLYQdhotXvAs2QXaW",
"credNum": "11440000006939799Q",
"delFlag": "0",
"type": "1",
"unitCode": "t9nq70CPS9PCwrL7ury",
"unitName": "市人社局"
},
{
"areaName": "中国",
"areaSeq": "VkThH0HKYsm6uLB1vSU",
"credNum": "11440000MB2D023437",
"delFlag": "0",
"type": "1",
"unitCode": "yK78GEa2MyLoTsv7cl8",
"unitName": "食药监局"
},
{
"areaName": "广州市",
"areaSeq": "nbaLYQdhotXvAs2QXaW",
"credNum": "11440000MB2D02159F",
"delFlag": "0",
"type": "1",
"unitCode": "rdF29TiDg8A6uAhtwy8",
"unitName": "市公安局"
},
{
"areaName": "广州市",
"areaSeq": "nbaLYQdhotXvAs2QXaW",
"credNum": "114400000069401674",
"delFlag": "0",
"type": "1",
"unitCode": "z5N2zSetiup81QAJzok",
"unitName": "市税务局"
},
{
"areaName": "广州市",
"areaSeq": "nbaLYQdhotXvAs2QXaW",
"delFlag": "0",
"parentUnitCode": "sVmQT735qD3Rwyu9wit",
"type": "2",
"unitCode": "6lMMRgrf6OBXe68P8C2",
"unitName": "研发事业部"
},
{
"areaName": "广州市",
"areaSeq": "nbaLYQdhotXvAs2QXaW",
"delFlag": "0",
"type": "1",
"unitCode": "sVmQT735qD3Rwyu9wit",
"unitName": "心猿码意软件"
}
]
实现代码:
/**
* <h2>窗口单位信息树</h2>
* @author 心猿码意
* @date 2023-04-25 18:23:21
**/
@Data
public class WinUnitTreeVo implements Serializable {
@ApiModelProperty(value = "所属地区名称")
private List<WinUnitTreeVo> childUnit;
@ApiModelProperty(value = "父级机构主键")
private String parentUnitCode;
@ApiModelProperty(value = "单位编号")
private String unitCode;
@ApiModelProperty(value = "单位名称")
private String unitName;
}
@Data
public class PlatformUnitVo {
/**
* 所属单位主键
*/
private String unitCode;
/**
* 部门名称
*/
private String unitName;
/**
* 父级机构主键
*/
private String parentUnitCode;
/**
* 是否主部门:0否,1是
*/
private String defaultFlag;
/**
* 社会统一信用代码
*/
private String credNum;
/**
* 删除状态:1删除,0否
*/
private String delFlag;
/**
* 单位所属地区编码
*/
private String areaSeq;
/**
* 单位所属地区名称
*/
private String areaName;
/**地区行政区划代码*/
private String areaCode;
/**
* 父级地区主键
*/
private String parentAreaSeq;
/**
* 父级地区名称
*/
private String parentAreaName;
/**
* 组织机构类型(1:单位,2:部门)
*/
private String type;
/**
* 附加属性值集合
*/
private List<ExtraAttrVo> extraAttrs;
}
public List<WinUnitTreeVo> getWorkitemUnitList(String unitCodes) {
List<PlatformUnitVo> platformUnitVos = "前面的json";
List<WinUnitTreeVo> winUnitTreeVoList = new ArrayList<>();
platformUnitVos.stream().forEach(vo ->{
WinUnitTreeVo winUnitTreeVo = new WinUnitTreeVo();
winUnitTreeVo.setUnitCode(vo.getUnitCode());
winUnitTreeVo.setUnitName(vo.getUnitName());
winUnitTreeVo.setParentUnitCode(vo.getParentUnitCode());
winUnitTreeVoList.add(winUnitTreeVo);
});
List<String> ids = winUnitTreeVoList.stream().map(WinUnitTreeVo::getUnitCode).collect(Collectors.toList());
List<WinUnitTreeVo> tree = new ArrayList<>();
for (WinUnitTreeVo winUnitTreeVo : winUnitTreeVoList) {
//找到根节点
if (winUnitTreeVo.getParentUnitCode() == null || !ids.contains(winUnitTreeVo.getParentUnitCode())) {
tree.add(findChildren(winUnitTreeVo, winUnitTreeVoList));
}
}
return tree;
}
private WinUnitTreeVo findChildren(WinUnitTreeVo winUnitTreeVo, List<WinUnitTreeVo> list) {
List<WinUnitTreeVo> children = new ArrayList<>();
for (WinUnitTreeVo node : list) {
if (StringUtils.isNotBlank(node.getParentUnitCode()) && node.getParentUnitCode().equals(winUnitTreeVo.getUnitCode())) {
//递归调用
children.add(findChildren(node, list));
}
}
winUnitTreeVo.setChildUnit(children);
return winUnitTreeVo;
}
最终返回的单位树如下:
[
{
"queryType": null,
"unitCode": "GEqONvGif1pkgeypVsW",
"unitName": "广东省残疾人联合会",
"userCode": null,
"parentUnitCode": null,
"childUnits": []
},
{
"queryType": null,
"unitCode": "27DYBhqxPhWkxyyShIZ",
"unitName": "测试",
"userCode": null,
"parentUnitCode": null,
"childUnits": [
{
"queryType": null,
"unitCode": "5coZCC2viVbTAotmTIW",
"unitName": "测试部门",
"userCode": null,
"parentUnitCode": "27DYBhqxPhWkxyyShIZ",
"childUnits": []
}
]
},
{
"queryType": null,
"unitCode": "N0mcwIHkncfZJG3jj0E",
"unitName": "广东省教育厅",
"userCode": null,
"parentUnitCode": null,
"childUnits": [
{
"queryType": null,
"unitCode": "k2OHsF7ZnpYADkJbiGi",
"unitName": "教育厅-1部门",
"userCode": null,
"parentUnitCode": "N0mcwIHkncfZJG3jj0E",
"childUnits": []
},
{
"queryType": null,
"unitCode": "TLgo7tY9HbD9TMZqRxL",
"unitName": "教育厅-1单位",
"userCode": null,
"parentUnitCode": "N0mcwIHkncfZJG3jj0E",
"childUnits": []
}
]
},
{
"queryType": null,
"unitCode": "pylto8i5gezxWPsLulf",
"unitName": "广州市市场监督管理局",
"userCode": null,
"parentUnitCode": null,
"childUnits": [
{
"queryType": null,
"unitCode": "dAVzZSmgFAuBhk05Tmw",
"unitName": "审批科",
"userCode": null,
"parentUnitCode": "pylto8i5gezxWPsLulf",
"childUnits": []
}
]
},
{
"queryType": null,
"unitCode": "8wUPbECBVx75UqQ6gjM",
"unitName": "文旅局",
"userCode": null,
"parentUnitCode": null,
"childUnits": [
{
"queryType": null,
"unitCode": "1yfUGoRc0EVyejByf9a",
"unitName": "市场处",
"userCode": null,
"parentUnitCode": "8wUPbECBVx75UqQ6gjM",
"childUnits": [
{
"queryType": null,
"unitCode": "iaqpYx0lUKzrum8oYMl",
"unitName": "事项测试部门43",
"userCode": null,
"parentUnitCode": "1yfUGoRc0EVyejByf9a",
"childUnits": []
}
]
},
{
"queryType": null,
"unitCode": "6DZWgfIUzHa0wk8ixKz",
"unitName": "广东省人力资源和社会保障厅",
"userCode": null,
"parentUnitCode": "8wUPbECBVx75UqQ6gjM",
"childUnits": [
{
"queryType": null,
"unitCode": "FMZpUJ3fCtnllZAAXPT",
"unitName": "测试部门-2-1",
"userCode": null,
"parentUnitCode": "6DZWgfIUzHa0wk8ixKz",
"childUnits": []
}
]
},
{
"queryType": null,
"unitCode": "hMAxUNUDfBdlRT6P5NT",
"unitName": "文物局",
"userCode": null,
"parentUnitCode": "8wUPbECBVx75UqQ6gjM",
"childUnits": []
},
{
"queryType": null,
"unitCode": "xWKRcZWjYTtuy5gMqmq",
"unitName": "审核处",
"userCode": null,
"parentUnitCode": "8wUPbECBVx75UqQ6gjM",
"childUnits": [
{
"queryType": null,
"unitCode": "P0ZjaRLjCaTmk1vCs0t",
"unitName": "事项测试子部门",
"userCode": null,
"parentUnitCode": "xWKRcZWjYTtuy5gMqmq",
"childUnits": []
}
]
},
{
"queryType": null,
"unitCode": "PCkQHkCqBQ8G433PlpN",
"unitName": "准入处",
"userCode": null,
"parentUnitCode": "8wUPbECBVx75UqQ6gjM",
"childUnits": []
}
]
},
{
"queryType": null,
"unitCode": "rBzaXFIRKovid7dgbyY",
"unitName": "密码局",
"userCode": null,
"parentUnitCode": null,
"childUnits": [
{
"queryType": null,
"unitCode": "KZlSpr0M9svbTxswtVp",
"unitName": "001部门",
"userCode": null,
"parentUnitCode": "rBzaXFIRKovid7dgbyY",
"childUnits": []
}
]
},
{
"queryType": null,
"unitCode": "EG3iCBPm7bldC2esW1f",
"unitName": "市民政局",
"userCode": null,
"parentUnitCode": null,
"childUnits": [
{
"queryType": null,
"unitCode": "RmCztFLB1Ip4Tl0Lcyk",
"unitName": "hello单位-1001部门",
"userCode": null,
"parentUnitCode": "EG3iCBPm7bldC2esW1f",
"childUnits": []
}
]
},
{
"queryType": null,
"unitCode": "wl4XDGhZg71ntFXT8aL",
"unitName": "市医保局",
"userCode": null,
"parentUnitCode": null,
"childUnits": [
{
"queryType": null,
"unitCode": "l29g1aszyjTCLmGgKMU",
"unitName": "one测试部门",
"userCode": null,
"parentUnitCode": "wl4XDGhZg71ntFXT8aL",
"childUnits": []
}
]
},
{
"queryType": null,
"unitCode": "t9nq70CPS9PCwrL7ury",
"unitName": "市人社局",
"userCode": null,
"parentUnitCode": null,
"childUnits": [
{
"queryType": null,
"unitCode": "IA6mo3clsYao9qRqVKB",
"unitName": "测试单位4-部门2",
"userCode": null,
"parentUnitCode": "t9nq70CPS9PCwrL7ury",
"childUnits": []
},
{
"queryType": null,
"unitCode": "wBaWrcxKXAORTsr7PXr",
"unitName": "测试单位4-部门1",
"userCode": null,
"parentUnitCode": "t9nq70CPS9PCwrL7ury",
"childUnits": []
}
]
},
{
"queryType": null,
"unitCode": "yK78GEa2MyLoTsv7cl8",
"unitName": "食药监局",
"userCode": null,
"parentUnitCode": null,
"childUnits": [
{
"queryType": null,
"unitCode": "oL5ku79eKjSD3wsonE2",
"unitName": "测试单位3-部门2",
"userCode": null,
"parentUnitCode": "yK78GEa2MyLoTsv7cl8",
"childUnits": []
},
{
"queryType": null,
"unitCode": "NZ7nrjiR2Ipp4jUvwdg",
"unitName": "测试单位3-部门1",
"userCode": null,
"parentUnitCode": "yK78GEa2MyLoTsv7cl8",
"childUnits": []
}
]
},
{
"queryType": null,
"unitCode": "rdF29TiDg8A6uAhtwy8",
"unitName": "市公安局",
"userCode": null,
"parentUnitCode": null,
"childUnits": [
{
"queryType": null,
"unitCode": "iAeyIFab9QYfW1qnqIB",
"unitName": "测试单位2-部门2",
"userCode": null,
"parentUnitCode": "rdF29TiDg8A6uAhtwy8",
"childUnits": []
},
{
"queryType": null,
"unitCode": "4AE4Eec19qlquekC67b",
"unitName": "测试单位2-部门1",
"userCode": null,
"parentUnitCode": "rdF29TiDg8A6uAhtwy8",
"childUnits": []
}
]
},
{
"queryType": null,
"unitCode": "z5N2zSetiup81QAJzok",
"unitName": "市税务局",
"userCode": null,
"parentUnitCode": null,
"childUnits": [
{
"queryType": null,
"unitCode": "PqIu1vdrPEMcgw9QLil",
"unitName": "测试单位-部门2",
"userCode": null,
"parentUnitCode": "z5N2zSetiup81QAJzok",
"childUnits": []
},
{
"queryType": null,
"unitCode": "IyfejZ9DXBX24OararK",
"unitName": "测试单位-部门1",
"userCode": null,
"parentUnitCode": "z5N2zSetiup81QAJzok",
"childUnits": []
}
]
},
{
"queryType": null,
"unitCode": "sVmQT735qD3Rwyu9wit",
"unitName": "心猿码意软件",
"userCode": null,
"parentUnitCode": null,
"childUnits": [
{
"queryType": null,
"unitCode": "6lMMRgrf6OBXe68P8C2",
"unitName": "研发事业部",
"userCode": null,
"parentUnitCode": "sVmQT735qD3Rwyu9wit",
"childUnits": []
}
]
}
]
到了这里,关于包含了父子关系的单位对象的List数据转成树状结构的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!