public static TreeVo findChild(TreeVo pt, List<TreeVo> treeAllDistinct) {
for (TreeVo ct : treeAllDistinct) {
if (ct.getParentId().equals(pt.getId())) {
if (pt.getChildren() == null) {
pt.setChildren(new ArrayList<>());
}
pt.getChildren().add(findChild(ct, treeAllDistinct));
}
}
return pt;
}
/**
* The entry point of application.
*
* @param args the input arguments
*/
public static void main(String[] args) {
List<TreeVo> treeVoList = initTree();
List<TreeVo> all = new ArrayList<>();
initTreeAllList(treeVoList, all);
System.out.println("转成List后的数据: " + JSON.toJSON(all));
List<TreeVo> distinct = all.stream().distinct().collect(Collectors.toList());
System.out.println("List去重后的数据: " + JSON.toJSON(distinct));
List<TreeVo> result = new ArrayList<>();
for (TreeVo treeVo : distinct) {
if (treeVo.getParentId().equals("0")) {
result.add(findChild(treeVo, distinct));
}
}
System.out.println("List转父子级后的数据: " + JSON.toJSON(result));
}
/**
* Init tree list.
*
* @return the list
*/
private static List<TreeVo> initTree() {
List<TreeVo> treeVoList = new ArrayList<>();
TreeVo t1 = new TreeVo();
t1.setId("1");
t1.setCode("code1");
t1.setName("name1");
t1.setType(0);
t1.setParentId("0");
List<TreeVo> t1ChildrenList = new ArrayList<>();
TreeVo t11 = new TreeVo();
t11.setId("11");
t11.setCode("code11");
t11.setName("name11");
t11.setType(0);
t11.setParentId("1");
TreeVo t12 = new TreeVo();
t12.setId("12");
t12.setCode("code12");
t12.setName("name12");
t12.setType(0);
t12.setParentId("1");
t1ChildrenList.add(t11);
t1ChildrenList.add(t12);
t1.setChildren(t1ChildrenList);
treeVoList.add(t1);
TreeVo t2 = new TreeVo();
t2.setId("2");
t2.setCode("code2");
t2.setName("name2");
t2.setType(0);
t2.setParentId("0");
List<TreeVo> t2ChildrenList = new ArrayList<>();
TreeVo t21 = new TreeVo();
t21.setId("21");
t21.setCode("code21");
t21.setName("name21");
t21.setType(0);
t21.setParentId("2");
TreeVo t22 = new TreeVo();
t22.setId("22");
t22.setCode("code22");
t22.setName("name22");
t22.setType(0);
t22.setParentId("2");
t2ChildrenList.add(t21);
t2ChildrenList.add(t22);
t2.setChildren(t2ChildrenList);
TreeVo t211 = new TreeVo();
t211.setId("211");
t211.setCode("code211");
t211.setName("name211");
t211.setType(1);
t211.setParentId("21");
List<TreeVo> t21ChildrenList = new ArrayList<>();
t21ChildrenList.add(t211);
t21.setChildren(t21ChildrenList);
treeVoList.add(t2);
System.out.println("初始带父子级的原数据: " + JSON.toJSON(treeVoList));
return treeVoList;
}
/**
* Init tree all list.
*
* @param treeVoList the tree vo list
* @param allList the all list
*/
public static void initTreeAllList(List<TreeVo> treeVoList, List<TreeVo> allList) {
for (TreeVo treeVo : treeVoList) {
TreeVo vo = new TreeVo();
vo.setId(treeVo.getId());
vo.setCode(treeVo.getCode());
vo.setName(treeVo.getName());
vo.setType(treeVo.getType());
vo.setRemark(treeVo.getRemark());
vo.setSortNo(treeVo.getSortNo());
vo.setParentId(treeVo.getParentId());
allList.add(vo);
List<TreeVo> children = treeVo.getChildren();
if (children != null && children.size() > 0) {
initTreeAllList(children, allList);
}
}
}
结果
初始带父子级的原数据: [{"code":"code1","children":[{"code":"code11","name":"name11","id":"11","type":0,"parentId":"1"},{"code":"code12","name":"name12","id":"12","type":0,"parentId":"1"}],"name":"name1","id":"1","type":0,"parentId":"0"},{"code":"code2","children":[{"code":"code21","children":[{"code":"code211","name":"name211","id":"211","type":1,"parentId":"21"}],"name":"name21","id":"21","type":0,"parentId":"2"},{"code":"code22","name":"name22","id":"22","type":0,"parentId":"2"}],"name":"name2","id":"2","type":0,"parentId":"0"}]
转成List后的数据: [{"code":"code1","name":"name1","id":"1","type":0,"parentId":"0"},{"code":"code11","name":"name11","id":"11","type":0,"parentId":"1"},{"code":"code12","name":"name12","id":"12","type":0,"parentId":"1"},{"code":"code2","name":"name2","id":"2","type":0,"parentId":"0"},{"code":"code21","name":"name21","id":"21","type":0,"parentId":"2"},{"code":"code211","name":"name211","id":"211","type":1,"parentId":"21"},{"code":"code22","name":"name22","id":"22","type":0,"parentId":"2"}]
List去重后的数据: [{"code":"code1","name":"name1","id":"1","type":0,"parentId":"0"},{"code":"code11","name":"name11","id":"11","type":0,"parentId":"1"},{"code":"code12","name":"name12","id":"12","type":0,"parentId":"1"},{"code":"code2","name":"name2","id":"2","type":0,"parentId":"0"},{"code":"code21","name":"name21","id":"21","type":0,"parentId":"2"},{"code":"code211","name":"name211","id":"211","type":1,"parentId":"21"},{"code":"code22","name":"name22","id":"22","type":0,"parentId":"2"}]
List转父子级后的数据: [{"code":"code1","children":[{"code":"code11","name":"name11","id":"11","type":0,"parentId":"1"},{"code":"code12","name":"name12","id":"12","type":0,"parentId":"1"}],"name":"name1","id":"1","type":0,"parentId":"0"},{"code":"code2","children":[{"code":"code21","children":[{"code":"code211","name":"name211","id":"211","type":1,"parentId":"21"}],"name":"name21","id":"21","type":0,"parentId":"2"},{"code":"code22","name":"name22","id":"22","type":0,"parentId":"2"}],"name":"name2","id":"2","type":0,"parentId":"0"}]