数据结构:id,name.......,pid;
根据id和pid生成树
/** * @clc * @param listMoiraiResource * @param id * 生成资源树,id传0,必须重根节点开始
入参:无序的list,返回正规树list * @return */ public static List<MoiraiResource> getResourceTree(List<MoiraiResource> listMoiraiResource, Long id) throws Exception{
if (StrUtils.isEmptyList(listMoiraiResource)) { return null; } List<MoiraiResource> list = new ArrayList<>(); List<MoiraiResource> listContinue = new ArrayList<>(listMoiraiResource); for (MoiraiResource mr : listMoiraiResource) { if (mr.getPid().equals(id)) { listContinue.remove(mr); mr.setChildren(getResourceTree(listContinue, mr.getResourceId())); list.add(mr); } } if (list.size() == 0) { return null; } return list; }