方法1:利用递归
下面是Dto类
public class DepartmentListDto {
private String id;
private String name;
private String parentId;
private String companyID;
private List<DepartmentListDto> list;
//getter、setter method ...
}
下面是接口方法
@RequestMapping("/getCompanyDeptList")
@ResponseBody
public MessagePacket getCompanyDeptList(HttpServletRequest request) throws ParseException{
List<Department> deptsTemp = this.departmentService.getList();
List<DepartmentListDto> depts = new ArrayList<>();
List<DepartmentListDto> nulllist = new ArrayList<DepartmentListDto>();
Map<String,Object> map = new HashMap<>();
for(Department department : deptsTemp){
DepartmentListDto departmentListDto = new DepartmentListDto();
departmentListDto.setId(department.getId());
departmentListDto.setName(department.getName());
departmentListDto.setParentId(department.getParentID());
departmentListDto.setList(nulllist);
depts.add(departmentListDto);
if(department.getParentID() !=null)
map.put(department.getParentID(),department);
}
List<DepartmentListDto> deptList = new ArrayList<DepartmentListDto>();
// 先找到所有的一级部门
for (int i = 0; i < depts.size(); i++) {
// 一级部门没有parentId
if ("hywater".equals(depts.get(i).getParentId()) &&
("003".equals(depts.get(i).getId())
|| "004".equals(depts.get(i).getId())
|| "005".equals(depts.get(i).getId())
|| "006".equals(depts.get(i).getId())
)
) {
deptList.add(depts.get(i));
}
}
// 为一级部门设置子部门,getChild是递归调用的
for (DepartmentListDto deptDto : deptList) {
deptDto.setList(getChildList(deptDto.getId(), depts, map));
}
JSONObject jsonObject = new JSONObject();
jsonObject.put("list",nlist);
return MessagePacket.newSuccess(jsonObject, "getCompanyDeptList success");
}
下面是获取子部门集合的递归方法
/**
*
* 递归部门
*/
private List<DepartmentListDto> getChildList(String id, List<DepartmentListDto> depts,Map<String,Object> map) {
// 子部门
List<DepartmentListDto> childList = new ArrayList<>();
for (DepartmentListDto dept : depts) {
// 遍历所有节点,将父部门id与传过来的id比较
if (StringUtils.isNotBlank(dept.getParentId())) {
if (dept.getParentId().equals(id)) {
childList.add(dept);
}
}
}
// 把子部门的子部门再循环一遍
for (DepartmentListDto dept : childList) {// map中存在的部门还有子部门
if (map.containsKey(dept.getId())) {
// 递归
dept.setList(getChildList(dept.getId(), depts,map));
}
} // 递归退出条件
if (childList.size() == 0) {
return null;
}
// 返回排序后的结果
return getSortChildren(childList);
}
下面是排序list集合的方法
/**
* 用匿名内部类排序list--有子部门的排序放到后面
* @param childList
* @return
*/
private List<DepartmentListDto> getSortChildren(List<DepartmentListDto> childList){
Collections.sort(childList, new Comparator<DepartmentListDto>() {
@Override
public int compare(DepartmentListDto o1, DepartmentListDto o2) {
if(o1.getList().size()>o2.getList().size()){
return 1;
}else{
return -1;
}
}
}) ;
return childList;
}
方法2:利用Map进行临时存储
package com;
import com.alibaba.fastjson.JSONObject;
import com.kingpivot.common.utils.DateUtils;
import com.kingpivot.common.utils.QueryFilter;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;
public class MyTest {
private int id;
private int depth;
private int pid;
private List<MyTest> myTestList;
public MyTest(int id,int depth,int pid){
this.id = id;
this.depth = depth;
this.pid = pid;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getDepth() {
return depth;
}
public void setDepth(int depth) {
this.depth = depth;
}
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public List<MyTest> getMyTestList() {
return myTestList;
}
public void setMyTestList(List<MyTest> myTestList) {
this.myTestList = myTestList;
}
@Override
public String toString() {
return "MyTest{" +
"id=" + id +
", depth=" + depth +
", pid=" + pid +
'}';
}
public static void main(String[] args) {
List<MyTest> list = new ArrayList<>();
/*
下面模拟一些数据
*/
for (int i = 1; i < 4; i++) {
MyTest myTest = new MyTest(i,2,0);
list.add(myTest);
}
for (int i = 10; i < 50; i++) {
if(i < 15){
MyTest myTest = new MyTest(i,3,1);
list.add(myTest);
}
if(i >= 15 && i < 20){
MyTest myTest = new MyTest(i,3,2);
list.add(myTest);
}
if(i >= 20 && i < 25){
MyTest myTest = new MyTest(i,3,3);
list.add(myTest);
}
if(i >= 25 && i < 30){
MyTest myTest = new MyTest(i,4,10);
list.add(myTest);
}
if(i >= 30 && i < 35){
MyTest myTest = new MyTest(i,4,15);
list.add(myTest);
}
if(i >= 35 && i < 40){
MyTest myTest = new MyTest(i,4,20);
list.add(myTest);
}
if(i >= 40){
MyTest myTest = new MyTest(i,5,35);
list.add(myTest);
}
}
/*
下面用两个map进行临时存储
*/
Map<Integer,List<MyTest>> map = new HashMap<>();
Map<Integer,List<MyTest>> dmap = new HashMap<>();
for (MyTest myTest : list) {
List<MyTest> maplist = map.get(myTest.getPid());
if(maplist == null){
maplist = new ArrayList<>();
map.put(myTest.getPid(),maplist);
}
maplist.add(myTest);
}
//移除顶级目录
map.remove(0);
List<MyTest> nlist = new ArrayList<>();
for (MyTest myTest : list) {
List<MyTest> maplist = map.get(myTest.getId());
List<MyTest> maplist2 = map.get(myTest.getPid());
if(maplist != null){
myTest.setMyTestList(maplist);
}
if(maplist2 == null){
nlist.add(myTest);
}
}
System.out.println(JSONObject.toJSONString(nlist));
}
}