数据结构
代码
/**
* 查询所有分类及子分类 , 以树形结构组装起来
* @return
*/
public List listWithTree() {
// 1 . 查询所有分类
List entities = baseMapper.selectList(null);
// 2 . 组装成父子的树形结构
// 2.1 找到所有的以及分类
/**
* Stream 是 Java8 中处理集合的关键抽象概念,它可以指定你希望对集合进行的操作,
* 可以执行非常复杂的查找、过滤和映射数据等操作。使用Stream API 对集合数据进行操作,
* 就类似于使用 SQL 执行的数据库查询。也可以使用 Stream API 来并行执行操作。简而言之,
* Stream API 提供了一种高效且易于使用的处理数据的方式。
*
*/
List leve1Menus = entities.stream().filter((categoryEntity)->{ // filter() : 过滤所有ParentCid = 0(父分类)
return categoryEntity.getParentCid() == 0 ;
}).map((menu)->{ //map() : 映射
menu.setChildren(getChildrens(menu , entities));
return menu;
}).sorted((menu1 , menu2)->{ //sorted() : 排序
return (menu1.getSort() == null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort()); //防止空指针异常
}).collect(Collectors.toList()); // collect() : 收集成一个list
return leve1Menus;
}
/**
* 递归找到所有父菜单的子菜单
* @param root 当前菜单
* @param all 所有菜单
* @return
*/
public List getChildrens(CategoryEntity root , List all){
List list = all.stream().filter((categoryEntity)->{
return categoryEntity.getParentCid() == root.getCatId(); //如果categoryEntity 的父id 是当前菜单的id 那么就说明 次菜单是当前菜单的子菜单
}).map((menu)->{
menu.setChildren(getChildrens(menu , all));
return menu;
}).sorted((menu1,menu2)->{
return (menu1.getSort() == null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort()); //防止空指针异常
}).collect(Collectors.toList());
return list;
}
效果
讲解
1 : 查询所有的父菜单
/**
* 查询所有分类及子分类 , 以树形结构组装起来
* @return
*/
public List listWithTree() {
// 1 . 查询所有分类
List entities = baseMapper.selectList(null);
// 2 . 组装成父子的树形结构
// 2.1 找到所有的以及分类
/**
* Stream 是 Java8 中处理集合的关键抽象概念,它可以指定你希望对集合进行的操作,
* 可以执行非常复杂的查找、过滤和映射数据等操作。使用Stream API 对集合数据进行操作,
* 就类似于使用 SQL 执行的数据库查询。也可以使用 Stream API 来并行执行操作。简而言之,
* Stream API 提供了一种高效且易于使用的处理数据的方式。
*
*/
List leve1Menus = entities.stream().filter((categoryEntity)->{ // filter() : 过滤所有ParentCid = 0(父分类)
return categoryEntity.getParentCid() == 0 ;
}).collect(Collectors.toList()); // collect() : 收集成一个list
return leve1Menus;
}
2 : 在实体类中增加children属性 , 查询所有父菜单的子菜单 , 并排序
/**
* 查询所有分类及子分类 , 以树形结构组装起来
* @return
*/
public List listWithTree() {
// 1 . 查询所有分类
List entities = baseMapper.selectList(null);
// 2 . 组装成父子的树形结构
// 2.1 找到所有的以及分类
/**
* Stream 是 Java8 中处理集合的关键抽象概念,它可以指定你希望对集合进行的操作,
* 可以执行非常复杂的查找、过滤和映射数据等操作。使用Stream API 对集合数据进行操作,
* 就类似于使用 SQL 执行的数据库查询。也可以使用 Stream API 来并行执行操作。简而言之,
* Stream API 提供了一种高效且易于使用的处理数据的方式。
*
*/
List leve1Menus = entities.stream().filter((categoryEntity)->{ // filter() : 过滤所有ParentCid = 0(父分类)
return categoryEntity.getParentCid() == 0 ;
}).map((menu)->{ //map() : 映射
menu.setChildren(getChildrens(menu , entities));
return menu;
}).sorted((menu1 , menu2)->{ //sorted() : 排序
return (menu1.getSort() == null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort());
}).collect(Collectors.toList()); // collect() : 收集成一个list
return leve1Menus;
}
/**
* 递归找到所有父菜单的子菜单
* @param root 当前菜单
* @param all 所有菜单
* @return list
*/
public List getChildrens(CategoryEntity root , List all){
List list = all.stream().filter((categoryEntity)->{
return categoryEntity.getParentCid() == root.getCatId(); //如果categoryEntity 的父id 是当前菜单的id 那么就说明 次菜单是当前菜单的子菜单
}).map((menu)->{
menu.setChildren(getChildrens(menu , all));
return menu;
}).sorted((menu1,menu2)->{
return (menu1.getSort() == null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort());
}).collect(Collectors.toList());
return list;
}