import java.util.ArrayList;
import java.util.List; public class TreeNode { private String id;
private String name;
private String parentId;
private String code;
private List<TreeNode> children; public TreeNode(String id, String name, String parentId, String code) {
this.id = id;
this.name = name;
this.parentId = parentId;
this.code = code;
} public void addChildren(TreeNode zone) {
if (children == null) {
children = new ArrayList<>();
}
children.add(zone);
} public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getParentId() {
return parentId;
} public void setParentId(String parentId) {
this.parentId = parentId;
} public String getCode() {
return code;
} public void setCode(String code) {
this.code = code;
} public List<TreeNode> getChildren() {
return children;
} public void setChildren(List<TreeNode> children) {
this.children = children;
} }
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors; import org.apache.commons.collections4.CollectionUtils; public class TreeUtils { /**
* 普通list转树状list
* @param treeNodeList
* @return
*/
public static List<TreeNode> list2Tree(List<TreeNode> treeNodeList) {
// Map<String, List<TreeNode>> zoneByParentIdMap = treeNodeList.stream().collect(Collectors.groupingBy(TreeNode::getParentId));
     // treeNodeList.forEach(zone->zone.children = zoneByParentIdMap.get(zone.id));
// return treeNodeList.stream().filter(v -> v.parentId.equals("0")).collect(Collectors.toList()); Map<String, List<TreeNode>> nodeByParentIdMap = treeNodeList.stream().collect(Collectors.groupingBy(TreeNode::getParentId));
treeNodeList.forEach(node->node.setChildren(nodeByParentIdMap.get(node.getId())));
return treeNodeList.stream().filter(v -> v.getParentId().equals("0")).collect(Collectors.toList());
} /**
* 树状list转普通list
* @param list
* @return
*/
public static List<TreeNode> tree2list(List<TreeNode> list) {
List<TreeNode> result = new ArrayList<>();
for (TreeNode test : list) {
List<TreeNode> c = test.getChildren();
result.add(test);
if (!CollectionUtils.isEmpty(c)) {
result.addAll(tree2list(c));
test.setChildren(null);//
}
}
return result;
} public static void main(String[] args) {
List<TreeNode> nodeList = new ArrayList<>();
nodeList.add(new TreeNode("10", "福建省","0", "FJS"));
nodeList.add(new TreeNode("11", "福州","10", "FZ"));
nodeList.add(new TreeNode("12", "莆田","10", "PT"));
nodeList.add(new TreeNode("13", "泉州","10", "QZ"));
nodeList.add(new TreeNode("14", "厦门","10", "XM"));
nodeList.add(new TreeNode("15", "龙岩","10", "LY"));
nodeList.add(new TreeNode("20", "浙江省","0", "ZJS"));
nodeList.add(new TreeNode("21", "杭州","20", "HZ"));
nodeList.add(new TreeNode("22", "嘉兴","20", "JX"));
nodeList.add(new TreeNode("23", "宁波","20", "NB"));
List<TreeNode> treeList = TreeUtils.list2Tree(nodeList);
List<TreeNode> list = TreeUtils.tree2list(treeList);
List<TreeNode> treeList1 = TreeUtils.list2Tree(list);
}
}

普通list和树状list互转的更多相关文章

  1. 【BZOJ-3648】寝室管理 环套树 + 树状数组 + 点分治

    3648: 寝室管理 Time Limit: 40 Sec  Memory Limit: 512 MBSubmit: 239  Solved: 106[Submit][Status][Discuss] ...

  2. HDU 4947 GCD Array 容斥原理+树状数组

    GCD Array Time Limit: 11000/5500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  3. HDU 4777 Rabbit Kingdom --容斥原理+树状数组

    题意: 给一个数的序列,询问一些区间,问区间内与区间其他所有的数都互质的数有多少个. 解法: 直接搞有点难, 所谓正难则反,我们求区间内与其他随便某个数不互质的数有多少个,然后区间长度减去它就是答案了 ...

  4. bzoj3529(莫比乌斯反演+离线+树状数组)

    在你以为理解mobus的时候,苦苦想通过化简公式来降低复杂度时,这题又打了我一巴掌. 看来我并没有理解到acmicpc比赛的宗旨啊. 这么多次查询可以考虑离线操作,使用树状数组单点更新. /***** ...

  5. BZOJ 3648: 寝室管理( 点分治 + 树状数组 )

    1棵树的话, 点分治+你喜欢的数据结构(树状数组/线段树/平衡树)就可以秒掉, O(N log^2 N). 假如是环套树, 先去掉环上1条边, 然后O(N log^2 N)处理树(同上); 然后再O( ...

  6. hdu 4777 树状数组+合数分解

    Rabbit Kingdom Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  7. hdu1540 Tunnel Warfare 线段树/树状数组

    During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast a ...

  8. 「模拟赛20180307」三元组 exclaim 枚举+树状数组

    题目描述 给定 \(n,k\) ,求有多少个三元组 \((a,b,c)\) 满足 \(1≤a≤b≤c≤n\)且\(a + b^2 ≡ c^3\ (mod\ k)\). 输入 多组数据,第一行数据组数\ ...

  9. 【BZOJ3529】【SDOI2014】数表 (莫比乌斯反演+树状数组)

    传送门 Description 有一张$n\times m$的数表,其第$i$行第$j$列 $(1≤i≤n,1≤j≤m)$ 的数值为能同时整除$i$和$j$的所有自然数之和.现在给定$a$,计算数表中 ...

随机推荐

  1. 深入理解Java虚拟机学习笔记(二)-----垃圾收集器与内存分配策略

    写在前面 本节常见面试题: 如何判断对象是否死亡(两种方法). 简单的介绍一下强引用.软引用.弱引用.虚引用(虚引用与软引用和弱引用的区别.使用软引用能带来的好处). 如何判断一个常量是废弃常量 如何 ...

  2. shell编程之系统环境变量2

    本课程是<Tony老师聊shell——变量>课程的延续,主要介绍Linux shell编程基础中的运算符.包括declare命令.数值运算方法和变量测试.首先在declare命令中介绍了数 ...

  3. Spring7——开发基于注解形式的spring

    开发基于注解形式的spring SpringIOC容器的2种形式: (1)xml配置文件:applicationContext.xml; 存bean:<bean> 取bean: Appli ...

  4. Build completed with 1 error and 0 warnings in 20 ms

    今天运行Idea,好端端的项目居然报了这个莫名其妙的错误Build completed with 1 error and 0 warnings in 20 ms. 首先排查下代码是否有问题,然后我就建 ...

  5. Java synthetic

    读完这篇文章你将会收获到 synthetic fields synthetic method synthetic class 概述 上一篇 Java 枚举 提及到编译成 class 文件之后.编译器会 ...

  6. CentOS7 安装rz和sz命令,安装netstat

    yum install lrzsz CentOS7 安装netstat命令 yum install net-tools

  7. Linux服务搭之 - 消息队列(RabbitMQ)

    本章主要目的是为了后续spring-cloud-bus做准备,讲述在Linux Centos7操作系统中搭建 RabbitMQ… - 什么是RabbitMQ RabbitMQ 是一个使用 Erlang ...

  8. ORA-04063: package body "DBSNMP.BSLN_INTERNAL" has errors

    ORA-04063: package body "DBSNMP.BSLN_INTERNAL" has errors 问题描述: 警告日志出现报错: Sun Jun 21 00:00 ...

  9. 基础-Junit单元测试_反射_注解

    一.Junit单元测试 1.1 测试分类: 黑盒测试:不需要写代码,给输入值,看程序是否能够输出期望的值. 白盒测试:需要写代码的.关注程序具体的执行流程. 1.2 Junit使用(白盒测试) 使用步 ...

  10. c语言学习笔记第四章——字符串和格式化输入、输出

    B站有视频演示 本章学习printf函数的输入输出,字符串的定义与实用. 字符串 字符串(character string)是一个或多个字符的序列,如下所示: "Zing went the ...