【mmall】递归查询子节点并排重
代码
@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】递归查询子节点并排重的更多相关文章
- oracle递归查询子节点
通过子节点向根节点追朔. select * from persons.dept start with deptid=76 connect by prior paredeptid=deptid 通过根节 ...
- MySQL递归查询父节点或递归查询子节点-陈远波
根据id查询父节点,具体需要修改的地方笔者已在注释中给大家作了注解 DELIMITER $$ USE `yjlc_platform`$$ -- getCompanyParent 为函数名 DROP F ...
- mysql自定义function 写递归查询子节点
#存储文本信息表 CREATE TABLE WordInfoEntity( word_id ) PRIMARY KEY NOT NULL, # 主键ID UUID word_greda :正文文本 , ...
- 包含mysql 递归查询父节点 和子节点
包含mysql 递归查询父节点 和子节点 mysql递归查询,查父集合,查子集合 查子集合 --drop FUNCTION `getChildList` CREATE FUNCTION `getChi ...
- MySQL递归查询树状表的子节点、父节点具体实现
mysql版本(5.5.6等等)尚未支持循环递归查询,和sqlserver.oracle相比,mysql难于在树状表中层层遍历的子节点.本程序重点参考了下面的资料,写了两个sql存储过程,子节点查询算 ...
- MySQL递归查询树状表的子节点、父节点
表结构和表数据就不公示了,查询的表user_role,主键是id,每条记录有parentid字段; 如下mysql查询函数即可实现根据一个节点查询所有的子节点,根据一个子节点查询所有的父节点.对于数据 ...
- (转)jQuery EasyUI Tree - TreeGrid动态加载子节点
有时我们已经得到充分的分层树形网格(TreeGrid)的数据. 我们还想让树形网格(TreeGrid)按层次惰性加载节点. 首先,只加载顶层节点. 然后点击节点的展开图标来加载它的子节点. 本教程展示 ...
- Easyui _treegrid 动态加载子节点
<table id="dg" class="easyui-treegrid" title="数据字典列表" data-options= ...
- jquery easyui tree动态加载子节点
1.前端tree绑定时,使用onBeforeExpand事件:当节点展开时触发加载子节点,自动会向服务端发送请求:url为绑定url,参数为当前节点id this.tree = { method: ' ...
随机推荐
- 第二十八篇-Fragment静态用法
效果图: 首先,先大致布局成这个形状 看动画中,横看分为两个区域,所以整体是一个水平排列 设置外层LinearLayout的参数 android:orientation="horizonta ...
- 使用text-align:justify,让内容两端对齐,兼容IE及主流浏览器的方法
如果不喜欢看分析过程,可以跳到最后看最终兼容方案 史前方法: 以前实现两端对齐是这样的: <p class="box1">密 码</p> <p cl ...
- qml: 模块定义与使用
1. 模块的定义qmldir Module MyModule MyTest 1.0 MyTest.qml MyTest 1.3 MyTest.qml 2. 导入: 使用qrc: RESOU ...
- bzoj1497 最小割
题意: 新的技术正冲击着手机通讯市场,对于各大运营商来说,这既是机遇,更是挑战.THU集团旗下的CS&T通讯公司在新一代通讯技术血战的前夜,需要做太多的准备工作,仅就站址选择一项,就需要完成前 ...
- Go GraphQL初学者教程
Go GraphQL初学者教程 https://tutorialedge.net/golang/go-graphql-beginners-tutorial/ https://tutorialedge. ...
- Django REST Framework限速
官方文档:http://www.django-rest-framework.org/api-guide/throttling/#throttling settings.py配置 REST_FRAMEW ...
- Elastic Stack之FileBeat使用实战
Elastic Stack之FileBeat使用实战 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本篇博客数据流走向:FileBeat ===>logstash == ...
- C#实现的系统内存清理
using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using S ...
- c++中sizeof的理解
1. 例题 #include <iostream> class A {}; class B { char m_data; }; class C { ]; }; class D { char ...
- python -- conda pytorch
Linux上用anaconda安装pytorch Pytorch是一个非常优雅的深度学习框架.使用anaconda可以非常方便地安装pytorch.下面我介绍一下用anaconda安装pytorch的 ...