树状结构工具
创建类TreeUtil
类属性

private List<T> rootList; //根节点对象存放到这里private List<T> bodyList; //其他节点存放到这里,可以包含根节点private Function<T,Object> treeId; //获取子级id接口private Function<T,Object> treeRootId;//获取上级id接口private BiConsumer<T,List> treeSetList;//用于setList的接口

两个构造方法

 /***  传入根节点与非根节点,实现三个函数接口* @param rootList 根节点* @param bodyList  非根节点* @param treeId    函数->获取id* @param treeRootId    ->获取根id* @param treeSetList   ->保存集合方法*/public TreeUtil(List<T> rootList, List<T> bodyList, Function treeId, Function treeRootId, BiConsumer treeSetList) {this.rootList = rootList;this.bodyList = bodyList;this.treeId = treeId;this.treeRootId = treeRootId;this.treeSetList = treeSetList;}/***传入集合,实现三个函数接口* @param salesAreaTrees 集合* @param treeId     函数->获取id* @param treeRootId    ->获取根id* @param treeSetList   ->保存集合方法*/public TreeUtil(List<T> salesAreaTrees, Function treeId, Function treeRootId, BiConsumer treeSetList) {this.treeId = treeId;this.treeRootId = treeRootId;this.treeSetList = treeSetList;//调用内部方法分离出根节点与非根节点this.getRootList(salesAreaTrees);}

分离方法

 /*** 得到RootList和BodyList** @param list*/private void getRootList(List<T> list) {rootList = new ArrayList<>();bodyList = new ArrayList<>();List<Object> ids = list.stream().parallel().map(p -> this.treeId.apply(p)).collect(Collectors.toList());List<Object> pids = list.stream().parallel().map(p -> this.treeRootId.apply(p)).collect(Collectors.toList());//(求id和pid的交集->用于推导非根节点 )List<Object> notRootPids = ids.stream().parallel().filter(id -> pids.contains(id)).collect(Collectors.toList());//非根节点rootList=list.stream().parallel().filter(item -> !notRootPids.contains(this.treeRootId.apply(item))).collect(Collectors.toList());//通过非根节点->根节点bodyList = list.stream().parallel().filter(item -> !rootList.contains(item)).collect(Collectors.toList());}

递归

public List<T> getTree() {   //调用的方法入口if (bodyList != null && !bodyList.isEmpty()) {//声明一个map,用来过滤已操作过的数据Map<Object, Object> map = Maps.newHashMapWithExpectedSize(bodyList.size());rootList.forEach(beanTree -> getChild(beanTree, map));return rootList;}return rootList;}private void getChild(T t, Map<Object, Object> map) {List<T> childList = new ArrayList<>();bodyList.stream().filter(c -> !map.containsKey(treeId.apply(c))).filter(c -> Objects.equals(this.treeRootId.apply(c),this.treeId.apply(t))).forEach(c -> {map.put(this.treeId.apply(c), this.treeRootId.apply(c));getChild(c, map);childList.add(c);});this.treeSetList.accept(t, childList);}

到此此工具类构建完毕.
使用示例:
构建函数

List<Tree> list=service.getList;Function<Tree,Long> function=Division::getId;Function<Tree,Long> function2=Division::getParentId;BiConsumer<Tree,List<Division>> treeSetList=Division::setChildList;

构建工具类

 TreeUtil<Tree> treeUtil= new TreeUtil<>(list,function,function2,treeSetList);

调用方法获得树状结构

treeUtil.getTree();

工具类完整代码

import com.google.common.collect.Maps;import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.stream.Collectors;![在这里插入图片描述](https://img-blog.csdnimg.cn/20190807155602797.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDUyNjQzMw==,size_16,color_FFFFFF,t_70)/*** @author 72060500* @version 1.0.1* 树状工具类*/
public class TreeUtil<T> {private List<T> rootList; //根节点对象存放到这里private List<T> bodyList; //其他节点存放到这里,可以包含根节点private Function<T,Object> treeId; //获取子级id接口private Function<T,Object> treeRootId;//获取上级id接口private BiConsumer<T,List> treeSetList;//用于setList的接口/***  传入根节点与非根节点,实现三个函数接口* @param rootList 根节点* @param bodyList  非根节点* @param treeId    函数->获取id* @param treeRootId    ->获取根id* @param treeSetList   ->保存集合方法*/public TreeUtil(List<T> rootList, List<T> bodyList, Function treeId, Function treeRootId, BiConsumer treeSetList) {this.rootList = rootList;this.bodyList = bodyList;this.treeId = treeId;this.treeRootId = treeRootId;this.treeSetList = treeSetList;}/***传入集合,实现三个函数接口* @param salesAreaTrees 集合* @param treeId     函数->获取id* @param treeRootId    ->获取根id* @param treeSetList   ->保存集合方法*/public TreeUtil(List<T> salesAreaTrees, Function treeId, Function treeRootId, BiConsumer treeSetList) {this.treeId = treeId;this.treeRootId = treeRootId;this.treeSetList = treeSetList;//调用内部方法分离出根节点与非根节点this.getRootList(salesAreaTrees);}public List<T> getTree() {   //调用的方法入口if (bodyList != null && !bodyList.isEmpty()) {//声明一个map,用来过滤已操作过的数据Map<Object, Object> map = Maps.newHashMapWithExpectedSize(bodyList.size());rootList.forEach(beanTree -> getChild(beanTree, map));return rootList;}return rootList;}private void getChild(T t, Map<Object, Object> map) {List<T> childList = new ArrayList<>();bodyList.stream().filter(c -> !map.containsKey(treeId.apply(c))).filter(c -> Objects.equals(this.treeRootId.apply(c),this.treeId.apply(t))).forEach(c -> {map.put(this.treeId.apply(c), this.treeRootId.apply(c));getChild(c, map);childList.add(c);});this.treeSetList.accept(t, childList);}/*** 得到RootList和BodyList** @param list*/private void getRootList(List<T> list) {rootList = new ArrayList<>();bodyList = new ArrayList<>();List<Object> ids = list.stream().parallel().map(p -> this.treeId.apply(p)).collect(Collectors.toList());List<Object> pids = list.stream().parallel().map(p -> this.treeRootId.apply(p)).collect(Collectors.toList());//(求id和pid的交集->用于推导非根节点 )List<Object> notRootPids = ids.stream().parallel().filter(id -> pids.contains(id)).collect(Collectors.toList());//非根节点rootList=list.stream().parallel().filter(item -> !notRootPids.contains(this.treeRootId.apply(item))).collect(Collectors.toList());//通过非根节点->根节点bodyList = list.stream().parallel().filter(item -> !rootList.contains(item)).collect(Collectors.toList());}
}

第一篇博客,记录一下

java构建树状结构工具类相关推荐

  1. JAVA构建树状结构

    记一下自己在展示树状结构时候的几个写法,一个是确定有几个层级的,用于只有两三个层级的树状结构,写法简单一点.还有就是不确定有几个层级,也可能1个,也可能4个或者更多. 先说一下确定有几个层级的写法 这 ...

  2. Java实现树状结构解析

    第一次实现了树状结构数据 记录自己的第一次 话不多说 直接上代码 数据库信息 代码实现 实体类 public class ChainTree implements Serializable {priv ...

  3. ssm中java实现树状结构_java ssm使用递归写树形结构

    实体类: private String dspId; private String parentId;  //父类id private String dataName; private Integer ...

  4. 层级关系java模型,树状结构Java模型、层级关系Java模型、上下级关系Java模型与html页面展示...

    tables; // 当前对象的下级集合 public Table(Long id) { this.id = id; // 方便快速创建对象 } } 1.组装Java模型数据 1)一次性从表里查出所有 ...

  5. 从装备合成谈树状结构的应用

    源码下载: 链接: https://pan.baidu.com/s/1DucpIik596W1pkY09FjUew 提取码: n8mq 其中有个文件"类的功能介绍.txt",介绍了 ...

  6. 树状结构搜索功能_ICLR 2020 高分论文!利用稀疏多层次Transformer结构获取语法树!...

    一.背景介绍 自从Tansformer结构提出以来,以BERT以代表的模型横扫NLP领域的各个任务.然而,Transformer中密集的注意力机制无法利用自然语言中的内在结构.这篇文章提出了一种新的T ...

  7. 【java工具类】四级菜单如何实现树状结构展示JSON给前端

    前言: 主要是传给前端进行树状结构操作比较好,这样可以关联上下四级选择项,所以需要把表中的数据List转成一个树状结构. 1.数据库数据是静态的四级目录 数据是这样的四级目录,没有特别明确的id进行关 ...

  8. java组装树状结构数据集合_JAVA构建List集合为树形结构

    package com.zving.tree; import java.util.ArrayList; import java.util.List; /** * 树形结构实体类 * @author c ...

  9. file类打印目录---树状结构,递归

    package Test; import java.io.File; /** * file类打印目录---树状结构,递归 * @author Administrator * */ public cla ...

最新文章

  1. 冬季巧食生姜可提高免疫力
  2. 简记模态对话框和非模态对话框
  3. 如何建立一个完整的游戏AI
  4. 信息学奥赛一本通(1131:基因相关性)
  5. 诗与远方:无题(六)
  6. STM32工作笔记0020---新建工程模板基于寄存器-M3
  7. 30 道 MySQL 面试题全放送!
  8. 第一次创业失败的总结 - 产品设计
  9. 需求管理系统分析与设计
  10. 十种经典运放电路分析
  11. 王者勋章服务器维护中,【维护】4月1日官方维护公告(抢鲜服)
  12. 不使用前端的pdf.js,通过pdfbox转换pdf为图片,拼接成html实现pdf前端预览
  13. 关于eclipse项目中项目上直接出现大红感叹号的问题
  14. 四川一度智信:如何做好店铺数据运营?
  15. ps高斯模糊出现白边
  16. 微信小程序获取input输入框的值
  17. 计算机图学实验报告,计算机图形学实验报告实验1
  18. mysql 金额_Mysql中金额使用DECIMAL类型
  19. 永信至诚打造企业安全人才提升方案 补齐企业安全短板
  20. 微信小程序:去掉button默认样式

热门文章

  1. 无法访问EChasrts官网的问题之电脑dns解析问题
  2. 谷歌软件工程师是怎样写设计文档的?
  3. 传智播客软件测试学习视频汇总:
  4. c++ 11 多线程支持 (std::packaged_task)
  5. 【直击DTCC】宝存CEO阳学仕:如何保障SSD的IO确定性?
  6. 语雀批量导出MarkDown文件
  7. [Pixhawk] ardupilot源码windows编译教程
  8. 机动目标运动分析——IMM篇
  9. IT行业为何如此吃香?2019学习IT就业前景分析
  10. 《学Unity的猫》——第十九集:皮皮猫上班第一天,认识游戏开发公司各个部门