一.在实际业务中常常需要将多级数据树化,传递给前端。
递归:资源消耗过大
steam流:需要多次手动设置foreach
stack:简单明了,消耗小,利用他的弹栈优点,不管树有多深都可以设置
二.省市区表
三.准备
(1)实体
(2)vo实体
四.stack方式
service业务代码
/**
* 所有查询地区
*/
@Override
public List<AreaVo> selectAll() {
//1.查询地区
List<Area> list = this.list(null);
List<AreaVo> allList = list.stream().map(item -> {
AreaVo areaVo = new AreaVo();
areaVo.setId(item.getId());
areaVo.setPid(item.getPid());
areaVo.setLabel(item.getAdcode());
areaVo.setValue(item.getName());
return areaVo;
}).collect(Collectors.toList());
//2.获取省
List<AreaVo> provinceList = allList.stream().filter(item -> item.getPid() == 0).collect(Collectors.toList());
List<AreaVo> areaVos = buildTreeByStack(allList, provinceList);
return areaVos;
}
public List<AreaVo> buildTreeByStack(List<AreaVo> treeNodes, List<AreaVo> parentNodes){
Stack<AreaVo> stack= new Stack<>();
//根入栈
parentNodes.forEach(e->stack.push(e));
List<AreaVo> root=new ArrayList<>();
while (!stack.isEmpty()){
AreaVo currentRoot = stack.pop();
List<AreaVo> children=new ArrayList<>();
Iterator<AreaVo> iterator = treeNodes.iterator();
while (iterator.hasNext()){
AreaVo next = iterator.next();
if(currentRoot.getId().toString().equals(next.getPid().toString())){
children.add(next);
iterator.remove();
}
}
currentRoot.setChildren(children);
if(!children.isEmpty()){
//子节点不为空入栈
children.forEach(e->stack.push(e));
}
if (currentRoot.getPid()==0){
root.add(currentRoot);
}
}
return root;
}
(4)结果
五.steam流
/**
* 所有查询地区
*/
@Override
public List<AreaVo> selectAll() {
String key = RedisConstants.COMMON_AREA;
//1.先去redis中取
List<AreaVo> areaList = redisCache.getCacheObject(key);
if (CollUtil.isNotEmpty(areaList)) {
return areaList;
}
List<AreaVo> tree = CollUtil.newLinkedList();
//1.查询地区
List<AreaVo> list = areaMapper.findAll();
Map<Long, List<AreaVo>> map = list.stream().collect(Collectors.groupingBy(AreaVo::getPid));
//得到省
list.stream().filter(vo -> vo.getPid() != null && vo.getPid() == 0).forEach(province -> {
List<AreaVo> cityList = map.get(province.getId());
if (CollUtil.isNotEmpty(cityList)) {
//市设置区
cityList.forEach(city -> city.setChildren(map.get(city.getId())));
}
province.setChildren(cityList);
//省设置市
tree.add(province);
});
if (CollUtil.isNotEmpty(tree)) {
redisCache.setCacheObject(key, tree);
}
return tree;
}