maven依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.qianyz</groupId>
<artifactId>SimpleDemo</artifactId>
<version>1.0-SNAPSHOT</version>
<name>SimpleDemo</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
拿部门举例,实体类代码如下
package com.qianyz.convertListToTree;
import lombok.Builder;
import lombok.Data;
import java.time.LocalDateTime;
@Data
@Builder
public class Department {
Integer id;
Integer pid;
String name;
String departmentCode;
LocalDateTime createTime;
}
功能简单剩下就一个类了
package com.qianyz.convertListToTree;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Main {
public static void main(String[] args) {
List<Department> list = init();
Map<Integer,List<Department>> map = new HashMap<>();
for(Department department : list){
List<Department> tempList = map.get(department.getPid());
if(tempList==null){
tempList = new ArrayList<>();
}
tempList.add(department);
map.put(department.getPid(),tempList);
}
List l = getReturn(-1,map);
JSONObject json = new JSONObject();
System.out.println(l);
System.out.println(JSONArray.toJSONString(l));
}
public static List<Map<String,Object>> getReturn(Integer pid, Map<Integer,List<Department>> map){
List<Map<String,Object>> returnList = new ArrayList<>();
List<Department> list = map.get(pid);
for(Department department : list){
Map<String,Object> hashMap = new HashMap<>();
hashMap.put("id",department.getId());
hashMap.put("pid",department.getPid());
hashMap.put("name",department.getName());
hashMap.put("departmentCode",department.getDepartmentCode());
hashMap.put("createTime",department.getCreateTime().toString());
if(map.containsKey(department.getId())){
hashMap.put("children",getReturn(department.getId(),map));
}
returnList.add(hashMap);
}
return returnList;
}
public static List<Department> init() {
List<Department> list = new ArrayList<>();
list.add(Department.builder().id(1).pid(-1).name("A公司").departmentCode("1000").createTime(LocalDateTime.now()).build());
list.add(Department.builder().id(2).pid(1).name("部门一").departmentCode("1100").createTime(LocalDateTime.now()).build());
list.add(Department.builder().id(3).pid(1).name("部门二").departmentCode("1200").createTime(LocalDateTime.now()).build());
list.add(Department.builder().id(4).pid(1).name("部门三").departmentCode("1300").createTime(LocalDateTime.now()).build());
list.add(Department.builder().id(5).pid(2).name("小组1").departmentCode("1110").createTime(LocalDateTime.now()).build());
list.add(Department.builder().id(6).pid(2).name("小组2").departmentCode("1120").createTime(LocalDateTime.now()).build());
list.add(Department.builder().id(7).pid(2).name("小组3").departmentCode("1130").createTime(LocalDateTime.now()).build());
list.add(Department.builder().id(8).pid(2).name("小组4").departmentCode("1140").createTime(LocalDateTime.now()).build());
list.add(Department.builder().id(9).pid(3).name("小队1").departmentCode("1210").createTime(LocalDateTime.now()).build());
list.add(Department.builder().id(10).pid(3).name("小队2").departmentCode("1220").createTime(LocalDateTime.now()).build());
list.add(Department.builder().id(11).pid(3).name("小队3").departmentCode("1230").createTime(LocalDateTime.now()).build());
list.add(Department.builder().id(12).pid(3).name("小队4").departmentCode("1240").createTime(LocalDateTime.now()).build());
list.add(Department.builder().id(13).pid(4).name("Team1").departmentCode("1310").createTime(LocalDateTime.now()).build());
list.add(Department.builder().id(14).pid(4).name("Team2").departmentCode("1320").createTime(LocalDateTime.now()).build());
list.add(Department.builder().id(15).pid(4).name("Team3").departmentCode("1330").createTime(LocalDateTime.now()).build());
list.add(Department.builder().id(16).pid(4).name("Team4").departmentCode("1340").createTime(LocalDateTime.now()).build());
return list;
}
}
最后输出了JSON格式的列表,暂时就先这样