Java实现树形结构转换器

Java实现树形结构转换器

import org.apache.commons.beanutils.BeanComparator;
import org.apache.commons.collections4.comparators.ComparatorChain;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 返回树型结构的一个转换器
 */
public class HierarchyConvertUtils {

    /**
     * @param list
     * @param rootId 第一层节点
     * @param <T>    id 为主键  parentId 为父节点 存放子节点的字段名称必须为childNode 且类型为java.util.List
     * @return
     */
    public static <T> List<T> convert(List<T> list, Object rootId) {
        List<T> result = new ArrayList<>();
        try {

            for (T model : list) {
                Class<?> aClass = model.getClass();
                Method roleName = aClass.getMethod("getParentId");
                if (rootId.equals(roleName.invoke(model))) {
                    Method childNodeMethod = aClass.getMethod("setChildNode", List.class);
                    Method idMethod = aClass.getMethod("getId");
                    T newModel = (T) BaseConvertUtils.toObject(model, model.getClass());
                    childNodeMethod.invoke(newModel, sortList(convert(list, idMethod.invoke(model))));
                    result.add(newModel);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("操作失败");
        }
        return sortList(result);
    }


    /**
     * 计算出层级hierarchy 例如 1 2 3  其中1为第一层级 依次递推
     *
     * @param list
     * @param rootId
     * @param <T>    必须包含hierarchy字段 类型 java.lang.Integer
     * @return
     */
    public static <T> List<T> convertHierarchy(List<T> list, Object rootId) {
        return convert(list, rootId, 1);
    }


    /**
     * @param list
     * @param rootId
     * @param h
     * @param <T>    id 为主键  parentId 为父节点 存放子节点的字段名称必须为childNode 且类型为java.util.List hierarchy为层次字段 类型java.lang.Integer
     * @return
     */
    private static <T> List<T> convert(List<T> list, Object rootId, Integer h) {
        List<T> result = new ArrayList<>();
        try {
            for (T model : list) {
                Class<?> aClass = model.getClass();
                Method roleName = aClass.getMethod("getParentId");
                if (rootId.equals(roleName.invoke(model))) {
                    Method childNodeMethod = aClass.getMethod("setChildNode", List.class);
                    Method idMethod = aClass.getMethod("getId");
                    T newModel = (T) BaseConvertUtils.toObject(model, model.getClass());
                    Method setHierarchy = aClass.getMethod("setHierarchy", Integer.class);
                    setHierarchy.invoke(newModel, h);
                    childNodeMethod.invoke(newModel, sortList(convert(list, idMethod.invoke(model), h + 1)));
                    result.add(newModel);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("操作失败");
        }
        return sortList(result);
    }

    /**
     * 默认 1 0 降序排序
     *
     * @param list
     * @param <T>
     * @return
     */
    private static <T> List<T> sortList(List<T> list) {
        ComparatorChain compChain = new ComparatorChain();
        compChain.addComparator(new BeanComparator("sort"), true);
        Collections.sort(list, compChain);
        return list;
    }

}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值