java转树形结构工具类详解
本文实例为大家分享了java转树形结构工具类的具体代码,供大家参考,具体内容如下
importcom.alibaba.fastjson.JSON;
importlombok.Data;
importlombok.ToString;
importorg.springframework.util.ReflectionUtils;
importorg.springframework.util.StringUtils;
importjava.lang.reflect.Field;
importjava.util.*;
/**
*@author:liyk
*@version1.0
*@date:2020/6/9
*/
publicclassTreeUtil{
/**
*将List转为树形结构
*
*@paramorigList:要转换的List
*@paramidFieldName:id字段名
*@paramparentIdFieldName:parentId字段名
*@paramchildrenFieldName:children字段名
*@param:拥有父子结构的Entity
*@return:树形结果
*@throwsException.
*/
publicstaticListconvert(ListorigList,StringidFieldName,
StringparentIdFieldName,StringchildrenFieldName)throwsException{
//用于保存当前id索引的实体类
MapidMaps=newHashMap<>();
//暂存区,用于保存没有找到父id的控件
ListtempList=newArrayList<>();
Listresult=newArrayList<>();
for(Tentity:origList){
//获取id,parentId,children
Stringid=Objects.toString(getFieldValue(entity,idFieldName),"");
StringparentId=Objects.toString(getFieldValue(entity,parentIdFieldName),"");
if(StringUtils.isEmpty(id)){
thrownewException("存在id为空的资料");
}
idMaps.put(id,entity);
if(StringUtils.isEmpty(parentId)){
//如果父id为空,则实体类为第一层
result.add(entity);
}else{
//根据父id获取实体类
TparentEntity=idMaps.get(parentId);
if(parentEntity==null){
//没找到先放入暂存区
tempList.add(entity);
}else{
//父组件判断是否存在children,不存在新增,存在则直接假如
setChildrenValue(childrenFieldName,entity,parentEntity);
}
}
}
//处理暂存区,暂存区的一定不为根节点,所以它只要父节点存在,那么此轮查询一定能找到父节点(上一轮已经将全部节点放入idMaps)
for(Tentity:tempList){
//获取parentId
StringparentId=Objects.toString(getFieldValue(entity,parentIdFieldName),"");
//根据父id获取实体类
TparentEntity=idMaps.get(parentId);
if(parentEntity==null){
thrownewException("存在孤立的子节点");
}else{
//父组件判断是否存在children,不存在新增,存在则直接假如
setChildrenValue(childrenFieldName,entity,parentEntity);
}
}
returnresult;
}
privatestaticvoidsetChildrenValue(StringchildrenFieldName,Tentity,TparentEntity)throwsException{
Objectchildren=getFieldValue(parentEntity,childrenFieldName);
ListchildrenList;
if(children==null){
childrenList=newArrayList<>();
childrenList.add(entity);
setFieldValue(parentEntity,childrenFieldName,childrenList);
}else{
ListchildrenReal=(List)children;
childrenReal.add(entity);
}
}
privatestaticObjectgetFieldValue(Tentity,StringfieldName)throwsException{
Fieldfield=ReflectionUtils.findField(entity.getClass(),fieldName);
if(field==null){
thrownewException(String.format("字段名称[%s]不存在",fieldName));
}
booleanaccessible=field.isAccessible();
field.setAccessible(true);
Objectresult=ReflectionUtils.getField(field,entity);
field.setAccessible(accessible);
returnresult;
}
privatestaticvoidsetFieldValue(Tentity,StringfieldName,Objectvalue)throwsException{
Fieldfield=ReflectionUtils.findField(entity.getClass(),fieldName);
if(field==null){
thrownewException(String.format("字段名称[%s]不存在",fieldName));
}
booleanaccessible=field.isAccessible();
field.setAccessible(true);
ReflectionUtils.setField(field,entity,value);
field.setAccessible(accessible);
}
publicstaticvoidmain(String[]args)throwsException{
Listlist=newArrayList<>();
for(inti=0;i<5;i++){
Demodemo=newDemo(i,"一级节点"+i);
list.add(demo);
}
for(inti=5;i<15;i++){
Demodemo=newDemo(i,i%5,"二级节点"+i);
list.add(demo);
}
for(inti=15;i<100;i++){
Demodemo=newDemo(i,i%10+5,"三级节点"+i);
list.add(demo);
}
Demodemo=newDemo(100,102,"非法节点");
list.add(demo);
Listconvert=TreeUtil.convert(list,"id","pid","children");
Strings=JSON.toJSONString(convert);
System.out.println(s);
}
}
@Data
@ToString
classDemo{
privateIntegerid;
privateIntegerpid;
privateStringname;
privateListchildren;
publicDemo(Integerid,Integerpid,Stringname){
this.id=id;
this.pid=pid;
this.name=name;
}
publicDemo(Integerid,Stringname){
this.id=id;
this.name=name;
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。