java轉(zhuǎn)樹形結(jié)構(gòu)工具類詳解
本文實(shí)例為大家分享了java轉(zhuǎn)樹形結(jié)構(gòu)工具類的具體代碼,供大家參考,具體內(nèi)容如下
import com.alibaba.fastjson.JSON;
import lombok.Data;
import lombok.ToString;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;
import java.lang.reflect.Field;
import java.util.*;
/**
* @author : liyk
* @version 1.0
* @date : 2020/6/9
*/
public class TreeUtil {
/**
* 將 List 轉(zhuǎn)為樹形結(jié)構(gòu)
*
* @param origList : 要轉(zhuǎn)換的 List
* @param idFieldName : id字段名
* @param parentIdFieldName : parentId 字段名
* @param childrenFieldName : children 字段名
* @param <T> : 擁有父子結(jié)構(gòu)的 Entity
* @return : 樹形結(jié)果
* @throws Exception .
*/
public static <T> List<T> convert(List<T> origList, String idFieldName,
String parentIdFieldName, String childrenFieldName) throws Exception {
// 用于保存當(dāng)前 id 索引的實(shí)體類
Map<String, T> idMaps = new HashMap<>();
// 暫存區(qū), 用于保存沒有找到父 id 的控件
List<T> tempList = new ArrayList<>();
List<T> result = new ArrayList<>();
for (T entity : origList) {
// 獲取 id, parentId, children
String id = Objects.toString(getFieldValue(entity, idFieldName), "");
String parentId = Objects.toString(getFieldValue(entity, parentIdFieldName), "");
if (StringUtils.isEmpty(id)) {
throw new Exception("存在id為空的資料");
}
idMaps.put(id, entity);
if (StringUtils.isEmpty(parentId)) {
// 如果父 id 為空, 則實(shí)體類為第一層
result.add(entity);
} else {
// 根據(jù)父 id 獲取實(shí)體類
T parentEntity = idMaps.get(parentId);
if (parentEntity == null) {
// 沒找到先放入暫存區(qū)
tempList.add(entity);
} else {
// 父組件判斷是否存在 children, 不存在新增, 存在則直接假如
setChildrenValue(childrenFieldName, entity, parentEntity);
}
}
}
// 處理暫存區(qū), 暫存區(qū)的一定不為根節(jié)點(diǎn), 所以它只要父節(jié)點(diǎn)存在, 那么此輪查詢一定能找到父節(jié)點(diǎn)(上一輪已經(jīng)將全部節(jié)點(diǎn)放入 idMaps)
for (T entity : tempList) {
// 獲取 parentId
String parentId = Objects.toString(getFieldValue(entity, parentIdFieldName), "");
// 根據(jù)父id獲取實(shí)體類
T parentEntity = idMaps.get(parentId);
if (parentEntity == null) {
throw new Exception("存在孤立的子節(jié)點(diǎn)");
} else {
// 父組件判斷是否存在children, 不存在新增, 存在則直接假如
setChildrenValue(childrenFieldName, entity, parentEntity);
}
}
return result;
}
private static <T> void setChildrenValue(String childrenFieldName, T entity, T parentEntity) throws Exception {
Object children = getFieldValue(parentEntity, childrenFieldName);
List<T> childrenList;
if (children == null) {
childrenList = new ArrayList<>();
childrenList.add(entity);
setFieldValue(parentEntity, childrenFieldName, childrenList);
} else {
List<T> childrenReal = (List<T>) children;
childrenReal.add(entity);
}
}
private static <T> Object getFieldValue(T entity, String fieldName) throws Exception {
Field field = ReflectionUtils.findField(entity.getClass(), fieldName);
if (field == null) {
throw new Exception(String.format("字段名稱[%s]不存在", fieldName));
}
boolean accessible = field.isAccessible();
field.setAccessible(true);
Object result = ReflectionUtils.getField(field, entity);
field.setAccessible(accessible);
return result;
}
private static <T> void setFieldValue(T entity, String fieldName, Object value) throws Exception {
Field field = ReflectionUtils.findField(entity.getClass(), fieldName);
if (field == null) {
throw new Exception(String.format("字段名稱[%s]不存在", fieldName));
}
boolean accessible = field.isAccessible();
field.setAccessible(true);
ReflectionUtils.setField(field, entity, value);
field.setAccessible(accessible);
}
public static void main(String[] args) throws Exception {
List<Demo> list = new ArrayList<>();
for (int i = 0; i < 5; i++) {
Demo demo = new Demo(i, "一級節(jié)點(diǎn)" + i);
list.add(demo);
}
for (int i = 5; i < 15; i++) {
Demo demo = new Demo(i, i % 5, "二級節(jié)點(diǎn)" + i);
list.add(demo);
}
for (int i = 15; i < 100; i++) {
Demo demo = new Demo(i, i % 10 + 5, "三級節(jié)點(diǎn)" + i);
list.add(demo);
}
Demo demo = new Demo(100, 102, "非法節(jié)點(diǎn)");
list.add(demo);
List<Demo> convert = TreeUtil.convert(list, "id", "pid", "children");
String s = JSON.toJSONString(convert);
System.out.println(s);
}
}
@Data
@ToString
class Demo {
private Integer id;
private Integer pid;
private String name;
private List<Demo> children;
public Demo(Integer id, Integer pid, String name) {
this.id = id;
this.pid = pid;
this.name = name;
}
public Demo(Integer id, String name) {
this.id = id;
this.name = name;
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Java遞歸遍歷樹形結(jié)構(gòu)的實(shí)現(xiàn)代碼
- 使用遞歸刪除樹形結(jié)構(gòu)的所有子節(jié)點(diǎn)(java和mysql實(shí)現(xiàn))
- Java創(chuàng)建樹形結(jié)構(gòu)算法實(shí)例代碼
- java實(shí)現(xiàn)省市區(qū)轉(zhuǎn)換成樹形結(jié)構(gòu)
- Java實(shí)現(xiàn)樹形結(jié)構(gòu)的示例代碼
- Java數(shù)據(jù)封裝樹形結(jié)構(gòu)代碼實(shí)例
- 使用遞歸算法結(jié)合數(shù)據(jù)庫解析成Java樹形結(jié)構(gòu)的代碼解析
- 詳解Java遞歸實(shí)現(xiàn)樹形結(jié)構(gòu)的兩種方式
- Java接口返回省市區(qū)樹形結(jié)構(gòu)的實(shí)現(xiàn)
相關(guān)文章
MyBatis查詢結(jié)果resultType返回值類型的說明
這篇文章主要介紹了MyBatis查詢結(jié)果resultType返回值類型的說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11
Mybatis Generator逆向工程的使用詳細(xì)教程
這篇文章主要介紹了Mybatis Generator逆向工程的使用詳細(xì)教程,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06
詳解IntelliJ IDEA中TortoiseSVN修改服務(wù)器地址的方法
這篇文章主要介紹了詳解IntelliJ IDEA中TortoiseSVN修改服務(wù)器地址的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12
解決在IDEA中創(chuàng)建多級package的問題
這篇文章主要介紹了解決在IDEA中創(chuàng)建多級package的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02
java實(shí)現(xiàn)動態(tài)數(shù)組
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)動態(tài)數(shù)組,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08
Mybatis使用useGeneratedKeys獲取自增主鍵的方法
這篇文章主要給大家介紹了關(guān)于Mybatis使用useGeneratedKeys獲取自增主鍵的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Mybatis具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
spring 使用RabbitMQ進(jìn)行消息傳遞的示例代碼
這篇文章主要介紹了spring 使用RabbitMQ進(jìn)行消息傳遞的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12
Springcloud Config支持本地配置文件的方法示例
這篇文章主要介紹了Springcloud Config支持本地配置文件的方法示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
maven項目install時忽略執(zhí)行test方法的總結(jié)
這篇文章主要介紹了maven項目install時忽略執(zhí)行test方法的總結(jié),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03

