JAVA8遍历tree

本文涉及的产品
可视分析地图(DataV-Atlas),3 个项目,100M 存储空间
简介: JAVA8遍历tree

1.数据库表image.png

  1. 代码

2.1
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

/** 字典树
*/
@Data
@Builder
@NoArgsConstructor
//@AllArgsConstructor
public class DictDataVo {
private int id;
private int idP;
private String dictLabel;
private String dictValue;
private String dictType;
private List childList;

public DictDataVo(int id, int idP, String dictLabel, String dictValue, String dictType){
    this.id = id;
    this.idP = idP;
    this.dictLabel = dictLabel;
    this.dictValue = dictValue;
}

public DictDataVo(int id, int idP, String dictLabel, String dictValue, String dictType, List<DictDataVo> childList){
    this.id = id;
    this.idP = idP;
    this.dictLabel = dictLabel;
    this.dictValue = dictValue;
    this.childList = childList;
}

}
2.2 实现

/****
 * 字典树
 * @param dictType
 * @return
 */
@Override
public List<DictDataVo> selectDictDataByTypeTree(String dictType) {

    //DictDataVo dictDataVo = DictDataVo.builder().dictType("fz_yj_jcbz").build();
    DictDataVo dictDataVo = new DictDataVo();
    dictDataVo.setDictType("fz_yj_jcbz");
    List<DictDataVo> dictDataVoList = dictDataMapper.selectDictTypeTreeList(dictDataVo);
    //父节点
    List<DictDataVo> collect = dictDataVoList.stream().filter(e -> e.getIdP()==0).map(
            (e) -> {
                e.setChildList(getChildrens(e, dictDataVoList));
                return e;
            }
    ).collect(Collectors.toList());

    return collect;
}


/***
 * 子节点
 * @param root
 * @param all
 * @return
 */
public List<DictDataVo> getChildrens(DictDataVo root, List<DictDataVo> all) {
    List<DictDataVo> childen = all.stream().filter(e -> {
        return Objects.equals(e.getIdP(), root.getId());
    }).map(
            (e) -> {
                e.setChildList(getChildrens(e, all));
                return e;
            }
    ).collect(Collectors.toList());
    return childen;
}
相关实践学习
基于Hologres轻松玩转一站式实时仓库
本场景介绍如何利用阿里云MaxCompute、实时计算Flink和交互式分析服务Hologres开发离线、实时数据融合分析的数据大屏应用。
阿里云实时数仓实战 - 项目介绍及架构设计
课程简介 1)学习搭建一个数据仓库的过程,理解数据在整个数仓架构的从采集、存储、计算、输出、展示的整个业务流程。 2)整个数仓体系完全搭建在阿里云架构上,理解并学会运用各个服务组件,了解各个组件之间如何配合联动。 3&nbsp;)前置知识要求 &nbsp; 课程大纲 第一章&nbsp;了解数据仓库概念 初步了解数据仓库是干什么的 第二章&nbsp;按照企业开发的标准去搭建一个数据仓库 数据仓库的需求是什么 架构 怎么选型怎么购买服务器 第三章&nbsp;数据生成模块 用户形成数据的一个准备 按照企业的标准,准备了十一张用户行为表 方便使用 第四章&nbsp;采集模块的搭建 购买阿里云服务器 安装 JDK 安装 Flume 第五章&nbsp;用户行为数据仓库 严格按照企业的标准开发 第六章&nbsp;搭建业务数仓理论基础和对表的分类同步 第七章&nbsp;业务数仓的搭建&nbsp; 业务行为数仓效果图&nbsp;&nbsp;
相关文章
|
1月前
|
Java
java实现遍历树形菜单方法——OpenSessionView实现
java实现遍历树形菜单方法——OpenSessionView实现
19 0
|
1月前
|
Java
java实现遍历树形菜单方法——TreeAction实现
java实现遍历树形菜单方法——TreeAction实现
14 0
|
1月前
|
Java
java实现遍历树形菜单方法——HibernateUtil实现
java实现遍历树形菜单方法——HibernateUtil实现
15 0
|
1月前
|
Java
java实现遍历树形菜单方法——service层
java实现遍历树形菜单方法——service层
15 0
|
1月前
|
Java
java实现遍历树形菜单方法——映射文件VoteTree.hbm.xml
java实现遍历树形菜单方法——映射文件VoteTree.hbm.xml
16 0
|
1月前
|
Java
java实现遍历树形菜单方法——index.jsp实现
java实现遍历树形菜单方法——index.jsp实现
11 0
|
1月前
|
Java
Tree Traversals Again(Java语言)(先序和中序创建二叉树)(遍历树)
Tree Traversals Again(Java语言)(先序和中序创建二叉树)(遍历树)
28 4
|
1天前
|
存储 算法 Java
Java中,树与图的算法涉及二叉树的前序、中序、后序遍历以及DFS和BFS搜索。
【6月更文挑战第21天】Java中,树与图的算法涉及二叉树的前序、中序、后序遍历以及DFS和BFS搜索。二叉树遍历通过访问根、左、右子节点实现。DFS采用递归遍历图的节点,而BFS利用队列按层次访问。以下是简化的代码片段:[Java代码略]
10 4
|
5天前
|
算法 Java 索引
【算法】重建二叉树并进行后序遍历的Java实现
【算法】重建二叉树并进行后序遍历的Java实现
17 5
|
12天前
|
存储 缓存 Java
Java遍历Map集合的方法
在Java中,遍历Map集合主要有四种方式:1) 使用`keySet()`遍历keys并用`get()`获取values;2) 使用`entrySet()`直接遍历键值对,效率较高;3) 通过`Iterator`遍历,适合在遍历中删除元素;4) Java 8及以上版本可用`forEach`和Lambda表达式,简洁易读。`entrySet()`通常性能最佳,而遍历方式的选择应考虑代码可读性和数据量。
28 0