代码

@Override
public ServerResponse getSelfAndChildrenCategory(Integer categoryId) {
    if (categoryId != null) return ServerResponse.error(ResponseCode.ILLEGAL_ARGUMENT.getCode(), "参数错误");
    Set<Category> categorySet = Sets.newHashSet(); // 初始化Set
    findChildCategory(categorySet, categoryId); // 递归
    List<Integer> categoryIdList = Lists.newArrayList(); // 初始化List
    for (Category categoryItem : categorySet) {
        categoryIdList.add(categoryItem.getId());
    }
    return ServerResponse.success(categoryIdList);
}

//递归算法,算出子节点
private Set<Category> findChildCategory(Set<Category> categorySet, Integer categoryId) {
    Category category = categoryMapper.selectByPrimaryKey(categoryId);
    // 将自己添加进去
    if (category != null) categorySet.add(category);
    //查找子节点,递归算法一定要有一个退出的条件
    List<Category> categoryList = categoryMapper.getChildrenCategory(categoryId);
    for (Category categoryItem : categoryList) {
        findChildCategory(categorySet, categoryItem.getId());
    }
    return categorySet;
}
/** 重写id的equals和hashCode方法,保证Set集合取出的值是不重复的 */
@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    Category category = (Category) o;

    return id.equals(category.id);
}

@Override
public int hashCode() {
    return id.hashCode();
}

参考文章

使用Set集合时:重写equals的同时为什么必须重写hashcode

【mmall】递归查询子节点并排重的更多相关文章

  1. oracle递归查询子节点

    通过子节点向根节点追朔. select * from persons.dept start with deptid=76 connect by prior paredeptid=deptid 通过根节 ...

  2. MySQL递归查询父节点或递归查询子节点-陈远波

    根据id查询父节点,具体需要修改的地方笔者已在注释中给大家作了注解 DELIMITER $$ USE `yjlc_platform`$$ -- getCompanyParent 为函数名 DROP F ...

  3. mysql自定义function 写递归查询子节点

    #存储文本信息表 CREATE TABLE WordInfoEntity( word_id ) PRIMARY KEY NOT NULL, # 主键ID UUID word_greda :正文文本 , ...

  4. 包含mysql 递归查询父节点 和子节点

    包含mysql 递归查询父节点 和子节点 mysql递归查询,查父集合,查子集合 查子集合 --drop FUNCTION `getChildList` CREATE FUNCTION `getChi ...

  5. MySQL递归查询树状表的子节点、父节点具体实现

    mysql版本(5.5.6等等)尚未支持循环递归查询,和sqlserver.oracle相比,mysql难于在树状表中层层遍历的子节点.本程序重点参考了下面的资料,写了两个sql存储过程,子节点查询算 ...

  6. MySQL递归查询树状表的子节点、父节点

    表结构和表数据就不公示了,查询的表user_role,主键是id,每条记录有parentid字段; 如下mysql查询函数即可实现根据一个节点查询所有的子节点,根据一个子节点查询所有的父节点.对于数据 ...

  7. (转)jQuery EasyUI Tree - TreeGrid动态加载子节点

    有时我们已经得到充分的分层树形网格(TreeGrid)的数据. 我们还想让树形网格(TreeGrid)按层次惰性加载节点. 首先,只加载顶层节点. 然后点击节点的展开图标来加载它的子节点. 本教程展示 ...

  8. Easyui _treegrid 动态加载子节点

    <table id="dg" class="easyui-treegrid" title="数据字典列表" data-options= ...

  9. jquery easyui tree动态加载子节点

    1.前端tree绑定时,使用onBeforeExpand事件:当节点展开时触发加载子节点,自动会向服务端发送请求:url为绑定url,参数为当前节点id this.tree = { method: ' ...

随机推荐

  1. 真机控件获取 app-inspector

    1.安装app-inspector:npm  install  app-inspector  -g 若是要卸载原有的:npm   uninstall   app-inspector   -g   np ...

  2. Java 接口篇

    为什么使用接口? 问题 要求实现防盗门的功能 分析 门有开和关的功能,锁有上锁和开锁的功能 将门和锁分别定义为抽奖类 那么问题就是防盗门即继承了门的同时又继承了锁,而Java的继承是单继承,接口可多继 ...

  3. TODO 动态执行appium代码,便于修改和调试

    https://testerhome.com/topics/9040 还没尝试过. 不过不是很懂怎么实现的,java不是编译后再运行的语言吗?怎么一边编译一边运行呢???

  4. noi.openjudge 1.12.6

    http://noi.openjudge.cn/ch0112/06/ 总时间限制:  2000ms 内存限制:  65536kB 描述 传说很遥远的藏宝楼顶层藏着诱人的宝藏.小明历尽千辛万苦终于找到传 ...

  5. 22. Generate Parentheses(ML)

    22. Generate Parentheses . Generate Parentheses Given n pairs of parentheses, write a function to ge ...

  6. JS(基础)_总结获取页面中元素和节点的方式

    一.前言 1.元素和节点的区别 2.总结获取元素的方式 3.总结获取节点的方式 二.主要内容 1.结点和元素的区别 (1)一些常见基本概念: 文档:document 元素:页面中所有的标签 结点:页面 ...

  7. C# string.format用法详解

    String.Format 方法的几种定义: String.Format (String, Object) 将指定的 String 中的格式项替换为指定的 Object 实例的值的文本等效项. Str ...

  8. Advertising.csv

    TV,radio,newspaper,sales1,230.1,37.8,69.2,22.12,44.5,39.3,45.1,10.43,17.2,45.9,69.3,9.34,151.5,41.3, ...

  9. springboot学习笔记-5 springboot整合shiro

    shiro是一个权限框架,具体的使用可以查看其官网 http://shiro.apache.org/  它提供了很方便的权限认证和登录的功能. 而springboot作为一个开源框架,必然提供了和sh ...

  10. Compiling R-3.4.3 on CentOS 6.10 with GNU

    If you are compiling CentOS 6, you will notice that the R source will not compile without a updated ...