|
@@ -6,6 +6,8 @@ import org.springframework.beans.BeanUtils;
|
|
|
|
|
|
import java.security.PublicKey;
|
|
import java.security.PublicKey;
|
|
import java.util.*;
|
|
import java.util.*;
|
|
|
|
+import java.util.function.BiConsumer;
|
|
|
|
+import java.util.function.Function;
|
|
|
|
|
|
public class ObjectUtil {
|
|
public class ObjectUtil {
|
|
|
|
|
|
@@ -108,4 +110,40 @@ public class ObjectUtil {
|
|
public static void voToPo(Object source, Object target){
|
|
public static void voToPo(Object source, Object target){
|
|
BeanUtils.copyProperties(source,target);
|
|
BeanUtils.copyProperties(source,target);
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * list转tree结构,
|
|
|
|
+ * @param list List<T>类型数据源
|
|
|
|
+ * @param rootId tree的根节点id
|
|
|
|
+ * @param getId <T>类型获取id的方法
|
|
|
|
+ * @param getPid <T>类型获取父id的方法
|
|
|
|
+ * @param getChildren <T>类型获取子对象的方法
|
|
|
|
+ * @param setChildren <T>类型设置子对象的方法
|
|
|
|
+ * @param <T> 该对象满足父子关系的相关字段:id 和 父id
|
|
|
|
+ * @param <String> id的数据类型为String
|
|
|
|
+ * @return 树形结构的List<T>,多根型
|
|
|
|
+ */
|
|
|
|
+ public static <T,String> List<T> listToTree(List<T> list, String rootId, Function<T, String> getId , Function<T, String> getPid, Function<T, List<T>> getChildren, BiConsumer<T,List<T>> setChildren) {
|
|
|
|
+ List<T> treeList = new ArrayList<T>();
|
|
|
|
+ for (T node : list) {
|
|
|
|
+ if (rootId.equals(getPid.apply(node))) {
|
|
|
|
+ treeList.add(findChildren(node, list,getId,getPid,getChildren,setChildren));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return treeList;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * list转tree递归调用
|
|
|
|
+ */
|
|
|
|
+ private static <T,String> T findChildren(T parent, List<T> list, Function<T, String> getId, Function<T, String> getPid, Function<T, List<T>> getChildren , BiConsumer<T,List<T>> setChildren) {
|
|
|
|
+ for (T node : list)
|
|
|
|
+ if (getId.apply(parent).equals(getPid.apply(node))) {
|
|
|
|
+ if (getChildren.apply(parent) == null || getChildren.apply(parent).size() == 0) {
|
|
|
|
+ setChildren.accept(parent, new ArrayList<T>());
|
|
|
|
+ }
|
|
|
|
+ getChildren.apply(parent).add(findChildren(node, list, getId, getPid, getChildren,setChildren));
|
|
|
|
+ }
|
|
|
|
+ return parent;
|
|
|
|
+ }
|
|
}
|
|
}
|