import lombok.Data;
import java.util.List;
@Data
public class Tree {
private String label;
private Long id;
private List<Tree> children;
}
属性数据组装工具类:
import com.space.common.core.domain.Tree;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
public class TreeUtil {
/**
* 获取树形数据
* @param data 数据
* @param parentIdMapper
* @param idMapper
* @param nameMapper
* @return
* */
public static <T,K,R> List<Tree> getTreeData(List<T> data, Function<? super T, ? extends K> parentIdMapper,TypeFunction<T,R> idMapper, TypeFunction<T,R> nameMapper) {
Map<?, List<T>> groupMap = data.stream().collect(Collectors.groupingBy(parentIdMapper));
List<T> datas = groupMap.get(0l);
List<Tree> children = new ArrayList<>();
return getTrees(children,datas,groupMap,idMapper,nameMapper);
}
/**
* 递归组装数据
* @param children 数据
* @param datas
* @param groupMap
* @param idMapper
* @param nameMapper
* @return
* */
private static <T,K,R> List<Tree> getTrees(List<Tree> children, List<T> datas, Map<?, List<T>> groupMap, TypeFunction<T,R> idMapper, TypeFunction<T,R> nameMapper) {
for (T child : datas) {
List<Tree> childrenOne = new ArrayList<>();
Tree childTree = new Tree();
Long childId = (Long) getAttributeValue(idMapper,child);
childTree.setId(childId);
childTree.setLabel((String) getAttributeValue(nameMapper,child));
if(groupMap.containsKey(childId)){
List<Tree> awardTrees = getTrees(childrenOne, groupMap.get(childId), groupMap,idMapper,nameMapper);
childTree.setChildren(awardTrees);
}else{
childTree.setChildren(new ArrayList<>());
}
children.add(childTree);
}
return children;
}
public static <T,R> Object getAttributeValue(TypeFunction<T,R> function,Object object){
return TypeFunction.getAttributeValue(function,object);
}
}
根据lambda表达式获取对象属性值:
import java.io.Serializable;
import java.lang.invoke.SerializedLambda;
import java.lang.reflect.Method;
import java.util.function.Function;
@FunctionalInterface
public interface TypeFunction<T, R> extends Serializable, Function<T, R> {
/**
* 获取属性值
* @param lambda
* @return String
*/
static Object getAttributeValue(Serializable lambda,Object object) {
try {
Method method = lambda.getClass().getDeclaredMethod("writeReplace");
method.setAccessible(Boolean.TRUE);
SerializedLambda serializedLambda = (SerializedLambda) method.invoke(lambda);
String getter = serializedLambda.getImplMethodName();
Method declaredMethod = object.getClass().getDeclaredMethod(getter);
return declaredMethod.invoke(object,new Object[]{});
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}
}
使用案例:
public static void main(String[] args) {
List<Dept> depts = new ArrayList<>();
Dept dept = new Dept();
dept.setDeptId(1l);
dept.setDeptName("组织");
dept.setParentId(0l);
depts.add(dept);
Dept dept2 = new Dept();
dept2.setDeptId(2l);
dept2.setDeptName("部门1");
dept2.setParentId(1l);
depts.add(dept2);
Dept dept3 = new Dept();
dept3.setDeptId(3l);
dept3.setDeptName("部门2");
dept3.setParentId(1l);
depts.add(dept3);
System.out.println(JSON.toJSONString(TreeUtil.getTreeData(depts,Dept::getParentId,Dept::getDeptId,Dept::getDeptName))); ;
}
打印结果:
[
{
"children":[
{
"children":[
],
"id":2,
"label":"部门1"
},
{
"children":[
],
"id":3,
"label":"部门2"
}
],
"id":1,
"label":"组织"
}
]