根据parentId获取所有下级数据

// 简单版

public List<Node> getChildrenById(List<Node> nodes, int parentId) {
    List<Node> children = new ArrayList<>();
    for (Node node : nodes) {
        if (node.getParentId() == parentId) {
            children.add(node);
            children.addAll(getChildrenById(nodes, node.getId()));
        }
    }
    return children;
}

// 优化版:使用一个visited集合来跟踪已经访问过的节点,防止重复访问

public List<Node> getChildrenById(List<Node> nodes, int parentId, Set<Integer> visited) {
    List<Node> children = new ArrayList<>();
    for (Node node : nodes) {
        if (node.getParentId() == parentId && !visited.contains(node.getId())) {
            visited.add(node.getId());
            children.add(node);
            children.addAll(getChildrenById(nodes, node.getId(), visited));
        }
    }
    return children;
}
// 迭代算法版,使用一个栈来存储待处理的节点,在每次循环中,我们从栈中弹出一个节点,并遍历所有节点,查找其子节点。如果找到了子节点,我们将其添加到子节点列表中,并将其压入栈中,以便后续处理。

public List<Node> getChildrenById(List<Node> nodes, int parentId) {
    List<Node> children = new ArrayList<>();
    Stack<Node> stack = new Stack<>();
    stack.push(new Node(parentId));
    while (!stack.isEmpty()) {
        Node node = stack.pop();
        for (Node child : nodes) {
            if (child.getParentId() == node.getId()) {
                children.add(child);
                stack.push(child);
            }
        }
    }
    return children;
}

不管哪个版本都要注意避免循环引用和死循环的问题

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然可以!下面是一个示例的分类列表接口的Java代码: ```java import java.util.ArrayList; import java.util.List; public class Category { private int id; private String name; private Integer parentId; private List<Category> children; // 构造函数 public Category(int id, String name, Integer parentId) { this.id = id; this.name = name; this.parentId = parentId; this.children = new ArrayList<>(); } // Getters and Setters public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getParentId() { return parentId; } public void setParentId(Integer parentId) { this.parentId = parentId; } public List<Category> getChildren() { return children; } public void setChildren(List<Category> children) { this.children = children; } // 添加子分类 public void addChild(Category child) { children.add(child); } } public class CategoryApi { private List<Category> categories; // 构造函数 public CategoryApi() { this.categories = new ArrayList<>(); initializeCategories(); } // 初始化分类数据 private void initializeCategories() { categories.add(new Category(1, "电子产品", null)); categories.add(new Category(2, "手机", 1)); categories.add(new Category(3, "电视", 1)); categories.add(new Category(4, "家具", null)); categories.add(new Category(5, "沙发", 4)); } // 获取分类列表 public List<Category> getCategoryList() { List<Category> rootCategories = new ArrayList<>(); for (Category category : categories) { if (category.getParentId() == null) { rootCategories.add(category); } else { Category parentCategory = findCategoryById(category.getParentId()); if (parentCategory != null) { parentCategory.addChild(category); } } } return rootCategories; } // 根据id查找分类 private Category findCategoryById(int id) { for (Category category : categories) { if (category.getId() == id) { return category; } } return null; } } // 测试接口 public class Main { public static void main(String[] args) { CategoryApi categoryApi = new CategoryApi(); List<Category> categoryList = categoryApi.getCategoryList(); for (Category category : categoryList) { printCategory(category, 0); } } // 递归打印分类树 private static void printCategory(Category category, int level) { StringBuilder indent = new StringBuilder(); for (int i = 0; i < level; i++) { indent.append(" "); } System.out.println(indent.toString() + "- " + category.getName()); for (Category child : category.getChildren()) { printCategory(child, level + 1); } } } ``` 这个接口使用了一个示例的分类列表数据,并提供了两个类:`Category`和`CategoryApi`。 `Category`类表示一个分类,包含`id`、`name`、`parentId`和子分类列表`children`属性。它还提供了添加子分类的方法`addChild`。 `CategoryApi`类是分类列表的API,它初始化了示例的分类数据,并提供了一个`getCategoryList`方法来获取分类列表。该方法会将分类数据构建为树形结构,并返回根节点列表。 在示例的`Main`类中,我们测试了接口,使用递归方式打印了分类树。 你可以根据实际需求,将这个示例代码进行修改和扩展。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值