public static List<ProductBriefCodePO> getTreeList(List<ProductBriefCodePO> list,String parentCode){
List<ProductBriefCodePO> rootNode = new ArrayList<>();
for (ProductBriefCodePO treeData: list ){
if (treeData.getCode().equals(parentCode)){
rootNode.add(treeData);
}
}
if (rootNode.size() == 0){
return getChild(parentCode,list);
}
for (ProductBriefCodePO treeData : rootNode){
String code = treeData.getCode();
treeData.setChildrenList(getChild(code,list));
}
return rootNode;
}
private static List<ProductBriefCodePO> getChild(String code, List<ProductBriefCodePO> list){
List<ProductBriefCodePO> childList = new ArrayList<>();
for (ProductBriefCodePO treeData: list){
if (treeData.getParentCode().equals(code)){
childList.add(treeData);
}
}
for (ProductBriefCodePO treeData : childList){
code = treeData.getCode();
treeData.setChildrenList(getChild(code,list));
}
if (childList.size() == 0){
return null;
}
return childList;
}
public class ProductBriefCodePO implements Serializable {
private Long id;
private String name;
private String code;
private String parentCode;
private Integer level;
private String remark;
private Integer versionNo;
private int isAvailable;
private int isDeleted;
private Long createUserid;
private String createUsername;
private Date createTime;
private Date createTimeDb;
private String serverIp;
private Long updateUserid;
private String updateUsername;
private Date updateTime;
private Date updateTimeDb;
private Long companyId;
private List<ProductBriefCodePO> childrenList;
public List<ProductBriefCodePO> getChildrenList() {
return childrenList;
}