2021-11-04父级末级节点工具类

父级末级节点工具类

public class TreeUtil {
  public static List<ProductCategory> getProductCategoryTreeList(List<ProductCategory> entityList) {

    List<ProductCategory> result = new ArrayList<>();

    //获取最上级数据集合
    for (ProductCategory entity : entityList) {

      Object parentId = entity.getParentId();

      //没有parentId代表最上级
      if (null == parentId || "".equals(parentId) || "0".equals(parentId)) {

        result.add(entity);
      }
    }

    //获取最上级子数据集合
    for (ProductCategory entity : result) {

      String id = entity.getId();

      List<ProductCategory> children = getProductCategoryChildren(id, entityList);

      if (null != children) {

        entity.setChildren(children);

      } else {

        entity.setChildren(new ArrayList<ProductCategory>());
      }
    }

    return result;
  }

  public static List<ProductCategory> getProductCategoryChildren(String id, List<ProductCategory> entityList) {

    List<ProductCategory> childList = new ArrayList<>();

    String parentId;

    //子集的子对象
    for (ProductCategory entity : entityList) {

      parentId = entity.getParentId();

      if (id.equals(parentId)) {

        childList.add(entity);
      }
    }

    for (ProductCategory entity : childList) {

      String childId = entity.getId();

      if (null != getProductCategoryChildren(childId, entityList)) {

        entity.setChildren(getProductCategoryChildren(childId, entityList));

      } else {

        entity.setChildren(new ArrayList<ProductCategory>());
      }
    }

    if (childList.size() == 0) {

      return null;
    }

    return childList;
  }




  public static List<Map<String, Object>> getTypeTreeList(List<Map<String, Object>> entityList) {

    List<Map<String, Object>> result = new ArrayList<>();

    //获取最上级数据集合
    for (Map<String, Object> entity : entityList) {

      Object parentId = entity.get("parentId");

      //没有parentId代表最上级
      if (null == parentId || "".equals(parentId) || "0".equals(parentId)) {

        result.add(entity);
      }
    }

    //获取最上级子数据集合
    for (Map<String, Object> entity : result) {

      String id = entity.get("key") + "";

      List<Map<String, Object>> children = getTypeChildren(id, entityList);

      if (null != children) {

        entity.put("children", children);

      } else {

        entity.put("children", new ArrayList<Map<String, Object>>());
      }
    }

    return result;
  }

  public static List<Map<String, Object>> getTypeChildren(String id, List<Map<String, Object>> entityList) {

    List<Map<String, Object>> childList = new ArrayList<>();

    String parentId;

    //子集的子对象
    for (Map<String, Object> entity : entityList) {

      parentId = entity.get("parentId") + "";

      if (id.equals(parentId)) {

        childList.add(entity);
      }
    }

    for (Map<String, Object> entity : childList) {

      String childId = entity.get("key") + "";

      if (null != getTypeChildren(childId, entityList)) {

        entity.put("children", getTypeChildren(childId, entityList));

      } else {

        entity.put("children", new ArrayList<Map<String, Object>>());
      }
    }

    if (childList.size() == 0) {

      return null;
    }

    return childList;
  }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值