一:樹結構實體
package com.it.tree;
import lombok.Data;
import lombok.experimental.Accessors;
import java.util.List;
/**
* 樹狀對象
*/
@Accessors(chain = true)
@Data
public class TreeData {
/**
* 節點標識
*/
private String id;
/**
* 是否是父節點
*/
private boolean isParent;
/**
* 父節點id
*/
private String parentId;
/**
* 節點上所需要的屬性(可根據需求增加和減少)
*/
private String data1;
/**
* 節點上所需要的屬性(可根據需求增加和減少)
*/
private String data2;
/**
* 節點上所需要的屬性(可根據需求增加和減少)
*/
private String data3;
/**
* 節點上所需要的屬性(可根據需求增加和減少)
*/
private String data4;
/**
* 節點上所需要的屬性(可根據需求增加和減少)
*/
private String data5;
/**
* 子節點集合
*/
private List<TreeData> childNodeList;
}
二:樹結構工具類和main方法測試(封裝數據)
package com.it.tree;
import com.google.common.collect.Lists;
import lombok.ToString;
import org.springframework.util.CollectionUtils;
import java.util.*;
@ToString
public class TreeUtils {
/**
* 通過構造器設置參數並處理返回樹狀數據
* @return
*/
public static List<TreeData> getResultTreeDataList(List<TreeData> rootNodeList, List<TreeData> childNodeList){
/** 子節點不爲空,封裝到對應的根節點下*/
if(!CollectionUtils.isEmpty(childNodeList)){
/**創建一個map用於保存已經處理過的childNodeList中的TreeData的id*/
Map<String, String> childNodesHashMap = new HashMap<>(childNodeList.size());
if(CollectionUtils.isEmpty(rootNodeList)){
return Lists.newArrayList();
}
rootNodeList.forEach(rootTreeData -> getChildList(childNodeList,rootTreeData,childNodesHashMap));
return rootNodeList;
}
return Lists.newArrayList();
}
private static void getChildList(List<TreeData> childNodeList,TreeData rootTreeData, Map<String, String> childNodesHashMap) {
/**用於保存子節點的數據*/
List<TreeData> childList = new ArrayList<>();
childNodeList.stream()
/**過濾掉已經處理過的childTreeData數據*/
.filter(childTreeData -> !childNodesHashMap.containsKey(childTreeData.getId()))
/**過濾掉根節點的子節點*/
.filter(childTreeData -> childTreeData.getParentId().equals(rootTreeData.getId()))
.forEach(childTreeData ->{
childNodesHashMap.put(childTreeData.getId(),childTreeData.getParentId());
getChildList(childNodeList,childTreeData,childNodesHashMap);
childList.add(childTreeData);
});
/**將子節點放入對應父節點的集合中*/
rootTreeData.setChildNodeList(childList);
}
public static void main(String[] args) {
/******************************************測試案例*****************************************************/
/**根節點集合*/
List<TreeData> rootNodeList = new ArrayList<>();
/**子節點集合*/
List<TreeData> childNodeList = new ArrayList<>();
/**科室*/
TreeData rootTreeDataofDepartMent = new TreeData().setId("1").setParent(true).setParentId(null).setData1("科室").setData2("科室人數").setData3("科室配置");
/**病區*/
TreeData rootTreeDataofInpatient = new TreeData().setId("2").setParent(true).setParentId(null).setData1("病區").setData2("病區人數").setData3("病區配置");
//根目錄集合
rootNodeList.add(rootTreeDataofDepartMent);
rootNodeList.add(rootTreeDataofInpatient);
/**科室*/
TreeData childTreeDataofDepartMent1 = new TreeData().setId("11").setParent(true).setParentId("1").setData1("科室1").setData2("科室人數1").setData3("科室配置1");
TreeData childTreeDataofDepartMent2 = new TreeData().setId("111").setParent(false).setParentId("11").setData1("科室11").setData2("科室人數11").setData3("科室配置11");
childNodeList.add(childTreeDataofDepartMent1);
childNodeList.add(childTreeDataofDepartMent2);
/**病區*/
TreeData childTreeDataofInpatient1 = new TreeData().setId("22").setParent(true).setParentId("2").setData1("病區2").setData2("病區人數2").setData3("病區配置2");
TreeData childTreeDataofInpatient2 = new TreeData().setId("222").setParent(false).setParentId("22").setData1("病區22").setData2("病區人數22").setData3("病區配置22");
childNodeList.add(childTreeDataofInpatient1);
childNodeList.add(childTreeDataofInpatient2);
List<TreeData> resultTreeDataList = TreeUtils.getResultTreeDataList(rootNodeList,childNodeList);
System.err.println(resultTreeDataList.toString());
}
}
三:執行main方法返回的結果(三層結構)
[TreeData(id=1, isParent=true, parentId=null, data1=科室, data2=科室人數, data3=科室配置, data4=null, data5=null,
childNodeList=[TreeData(id=11, isParent=true, parentId=1, data1=科室1, data2=科室人數1, data3=科室配置1, data4=null, data5=null,
childNodeList=[TreeData(id=111, isParent=false, parentId=11, data1=科室11, data2=科室人數11, data3=科室配置11, data4=null, data5=null, childNodeList=[])])]),
TreeData(id=2, isParent=true, parentId=null, data1=病區, data2=病區人數, data3=病區配置, data4=null, data5=null,
childNodeList=[TreeData(id=22, isParent=true, parentId=2, data1=病區2, data2=病區人數2, data3=病區配置2, data4=null, data5=null,
childNodeList=[TreeData(id=222, isParent=false, parentId=22, data1=病區22, data2=病區人數22, data3=病區配置22, data4=null, data5=null, childNodeList=[])])])]