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ū), 用于保存沒(méi)有找到父 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) {
// 沒(méi)找到先放入暫存區(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, "一級(jí)節(jié)點(diǎn)" + i);
list.add(demo);
}
for (int i = 5; i < 15; i++) {
Demo demo = new Demo(i, i % 5, "二級(jí)節(jié)點(diǎn)" + i);
list.add(demo);
}
for (int i = 15; i < 100; i++) {
Demo demo = new Demo(i, i % 10 + 5, "三級(jí)節(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;
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(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ù)庫(kù)解析成Java樹形結(jié)構(gòu)的代碼解析
- 詳解Java遞歸實(shí)現(xiàn)樹形結(jié)構(gòu)的兩種方式
- Java接口返回省市區(qū)樹形結(jié)構(gòu)的實(shí)現(xiàn)
相關(guān)文章
Java簡(jiǎn)單實(shí)現(xiàn)猜數(shù)字游戲附C語(yǔ)言版本
猜數(shù)字是興起于英國(guó)的益智類小游戲,起源于20世紀(jì)中期,一般由兩個(gè)人或多人玩,也可以由一個(gè)人和電腦玩。游戲規(guī)則為一方出數(shù)字,一方猜,今天我們來(lái)用Java和C語(yǔ)言分別把這個(gè)小游戲?qū)懗鰜?lái)練練手2021-11-11
spring boot入門開始你的第一個(gè)應(yīng)用
這篇文章主要介紹了spring boot入門開始你的第一個(gè)應(yīng)用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,,需要的朋友可以參考下2019-06-06
Java BufferedImage轉(zhuǎn)換為MultipartFile方式
這篇文章主要介紹了Java BufferedImage轉(zhuǎn)換為MultipartFile方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
解決MyEclipse出現(xiàn)the user operation is waiting的問(wèn)題
今天做項(xiàng)目的時(shí)候每次修改代碼保存后都會(huì)跳出一個(gè)框框,然后就有兩個(gè)進(jìn)度條,上面寫the user operation is wating...小編去網(wǎng)上查了查解決了這個(gè)問(wèn)題,下面跟大家分享一下。2018-04-04
springboot整合 xxl-job的項(xiàng)目實(shí)踐
XL-JOB是一個(gè)分布式任務(wù)調(diào)度平臺(tái),用于解決分布式系統(tǒng)中的任務(wù)調(diào)度和管理問(wèn)題,它包括調(diào)度中心、執(zhí)行器和Web管理控制臺(tái),本文就來(lái)介紹一下springboot整合 xxl-job的項(xiàng)目實(shí)踐,感興趣的可以了解一下2024-09-09
java?random隨機(jī)數(shù)的用法及常見應(yīng)用場(chǎng)景
這篇文章主要給大家介紹了關(guān)于java?random隨機(jī)數(shù)的用法及常見應(yīng)用場(chǎng)景的相關(guān)資料,Java中的Random類是用來(lái)生成偽隨機(jī)數(shù)的工具類,它可以用來(lái)生成隨機(jī)的整數(shù)、浮點(diǎn)數(shù)和布爾值,需要的朋友可以參考下2023-11-11
springboot 使用yml配置文件給靜態(tài)變量賦值教程
這篇文章主要介紹了springboot 使用yml配置文件給靜態(tài)變量賦值教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04

