package com.infish.util;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; /**
* @author wxl
* @Title: TreeUtil
* @Description: 通用Util,根据父节点关系将列表转树形结构
* @date 2018-12-10 17:54
*/
public class TreeUtil { /**
* @Description: 首字母转大写
* @param
* @return
* @throws
* @author wxl
* @date 2018-12-10 20:44
*/
public static String firstCharToUpperCase(String str){
if(str == null){ return null; }
if("".equals(str)){ return ""; }
char[] cs = str.toCharArray();
cs[0]-=32;
return String.valueOf(cs);
} /** 调用示例:示例: List<TreeNode> head = toTree(TreeNode.class,nodeList,"id","parentId","children",0L);
* @Description: List转Tree
* @param clazz 类.class
* @param dataList 数据列表
* @param idField ID字段名称
* @param parentIdField 父节点ID字段名称
* @param childrenField 孩子字段名称
* @param rootFlag 如果某个节点的ParentId等于rootFlag,即改节点为根节点
* @return
* @throws
* @author wxl
* @date 2018-12-10 20:44
*/
public static <T> List<T> toTree(Class<T> clazz,List<T> dataList,String idField,String parentIdField,String childrenField,Object rootFlag) throws Exception {
List<T> treeList = new ArrayList<>();
Map<Object,T> map = new HashMap<>();
Map<Object,Boolean> flag = new HashMap<>();
String getIdStr = "get"+firstCharToUpperCase(idField);
String getParentIdStr = "get"+firstCharToUpperCase(parentIdField);
String getChildrenStr = "get"+firstCharToUpperCase(childrenField);
String setChildrenStr = "set"+firstCharToUpperCase(childrenField);
Method getIdMethod = null;
Method getParentIdMethod=null;
Method getChildrenMethod=null;
Method setChildrenMethod=null;
try{
getIdMethod = clazz.getDeclaredMethod(getIdStr);
getParentIdMethod = clazz.getDeclaredMethod(getParentIdStr);
getChildrenMethod = clazz.getDeclaredMethod(getChildrenStr);
setChildrenMethod = clazz.getDeclaredMethod(setChildrenStr,List.class);
}catch (Exception e){
e.printStackTrace();
throw new RuntimeException("转树形结构-通过反射获取方法错误!",e);
} for(T node:dataList){
map.put(getIdMethod.invoke(node),node);
Object parentIdTmp = getParentIdMethod.invoke(node);
if(rootFlag==null || "".equals(rootFlag)){
if(parentIdTmp==null || "".equals(parentIdTmp)){
treeList.add(node);
}
}else{
if(rootFlag.equals(parentIdTmp)){
treeList.add(node);
}
}
}
for(T node:dataList){
T parentNode = map.get(getParentIdMethod.invoke(node));
if(parentNode!=null){
List<T> invoke = (List<T>)getChildrenMethod.invoke(parentNode);
if(invoke==null){
invoke=new ArrayList<>();
setChildrenMethod.invoke(parentNode,invoke);
}
invoke.add(node); }
}
return treeList;
} /* public static void main(String[] args) throws Exception {
List<TreeNode> sourceList = new ArrayList<>();
TreeNode n1 = new TreeNode(); n1.setId(1);n1.setParentId(null); sourceList.add(n1);
TreeNode n2 = new TreeNode(); n2.setId(2);n2.setParentId(1); sourceList.add(n2);
TreeNode n3 = new TreeNode(); n3.setId(3);n3.setParentId(2); sourceList.add(n3);
List<TreeNode> treeNodes = toTree(TreeNode.class, sourceList, "id", "parentId", "children", null);
treeNodes.forEach(n-> System.out.println(n));
}*/
}

TreeUtil.java的更多相关文章

  1. 【原】无脑操作:EasyUI Tree实现左键只选择叶子节点、右键浮动菜单实现增删改

    Easyui中的Tree组件使用频率颇高,经常遇到的需求如下: 1.在树形结构上,只有叶子节点才能被选中,其他节点不能被选中: 2.在叶子节点上右键出现浮动菜单实现新增.删除.修改操作: 3.在非叶子 ...

  2. Spark案例分析

    一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...

  3. java 根据 根节点及所有子成员 构造树tree

    实体类entity package com.ompa.biz.entity; import java.util.ArrayList; import java.util.List; public cla ...

  4. java递归 处理权限管理菜单树或分类

    1.数据库表设计 2.实体类设计 package com.ieou.capsule.dto.SystemPermissions; import java.util.List; /** * 功能菜单类 ...

  5. Atitit。Tree文件解析器的原理流程与设计实现  java  c# php js

    Atitit.Tree文件解析器的原理流程与设计实现  java  c# php js 1. 解析原理与流程1 1.1. 判断目录  ,表示服  dirFlagChar = "└├─&quo ...

  6. Java生成菜单树(目录树)的几种方式

    本文介绍两种不同生成多级目录树的方式:1. 递归生成,2.  map+list 集合生成.最下方会附上完整代码. 生成树的基本规则:子节点的par_id等于父节点的id. 1. 实体类 import ...

  7. 故障重现(内存篇2),JAVA内存不足导致频繁回收和swap引起的性能问题

    背景起因: 记起以前的另一次也是关于内存的调优分享下   有个系统平时运行非常稳定运行(没经历过大并发考验),然而在一次活动后,人数并发一上来后,系统开始卡. 我按经验开始调优,在每个关键步骤的加入如 ...

  8. Elasticsearch之java的基本操作一

    摘要   接触ElasticSearch已经有一段了.在这期间,遇到很多问题,但在最后自己的不断探索下解决了这些问题.看到网上或多或少的都有一些介绍ElasticSearch相关知识的文档,但个人觉得 ...

  9. 论:开发者信仰之“天下IT是一家“(Java .NET篇)

    比尔盖茨公认的IT界领军人物,打造了辉煌一时的PC时代. 2008年,史蒂夫鲍尔默接替了盖茨的工作,成为微软公司的总裁. 2013年他与微软做了最后的道别. 2013年以后,我才真正看到了微软的变化. ...

随机推荐

  1. 教你写百分九十的shell

    本文章主要内容来自菜鸟教程 , 也添加了一些知识点 shell脚本? 在说什么是shell脚本之前,先说说什么是shell.shell是外壳的意思,就是操作系统的外壳.我们可以通过shell命令来操作 ...

  2. zset如何解决内部链表查找效率低下

    zset作为有序集合,内部基于跳表或者说索引的方式实现了数据的快速查找.解决了链表查询效率低下的痛点 前言 紧接前文我们学习了Redis中Hash结构.在里面我们梳理了字典这个重要的内部结构并分析了h ...

  3. SpringBoot缓存管理(二) 整合Redis缓存实现

    SpringBoot支持的缓存组件 在SpringBoot中,数据的缓存管理存储依赖于Spring框架中cache相关的org.springframework.cache.Cache和org.spri ...

  4. 资源:Intellij IDEA 最新旗舰版注册激活破解*附注册码(2020年亲测)

    永久激活 1. 下载jetbrains-agent.jar包(2020.03.22亲测) 链接: https://pan.baidu.com/s/1BFXPwlROEF03BkhGzGIgEA 提取码 ...

  5. SpringMVC(12)完结篇 基于Hibernate+Spring+Spring MVC+Bootstrap的管理系统实现

    到这里已经写到第12篇了,前11篇基本上把Spring MVC主要的内容都讲了,现在就直接上一个项目吧,希望能对有需要的朋友有一些帮助. 一.首先看一下项目结构: InfrastructureProj ...

  6. ctf实验吧Once More

    题目链接:http://ctf5.shiyanbar.com/web/more.php 思路分析:显然是后台逻辑代码. 1.ereg函数有漏洞,可以使用%00截断,这个就做笔记了好吧.这个函数大致意思 ...

  7. 交换机卡在CPU task进程处理方法

    故障现象: 笔记本通过console线连接H3C交换机的console口,无法登陆,敲任何东西都无效.因为没有备份,不敢重启.显示以下报错: <test-sw> wrong input! ...

  8. C语言:结构体应用

    #include <stdio.h> #include <stdlib.h> #include <assert.h> typedef struct student{ ...

  9. C语言:int -32768-32767

    c语言中int的表示范围是-32768~32767!这得从二进制的原码说起:如果以最高位为符号位,二进制原码最大为0111111111111111=2的15次方减1=32767最小为111111111 ...

  10. C语言枚举类型

    在实际编程中,有些数据的取值往往是有限的,只能是非常少量的整数,并且最好为每个值都取一个名字,以方便在后续代码中使用,比如一个星期只有七天,一年只有十二个月,一个班每周有六门课程等.以每周七天为例,我 ...