@Data
@Accessors(chain = true)
public class Dept {
private String code;
private String name;
private String parentCode;
private List<Dept> item;
private int userNum;
private List<User> userItem;
}
@Data
@Accessors(chain = true)
public class User {
private String code;
private String name;
private String deptCode;
}
public class TreeTest {
public static void main(String[] args) {
List<Dept> dept = initDept();
List<User> user = initUser();
List<Dept> tree = tree(dept, user, true);
System.out.println(JsonUtils.beanToJson(tree));
// https://www.sojson.com/simple_json.html
}
/**
* 用户与部门树形结构
*
* @param dept 部门集合
* @param user 用户集合
* @param clearZero 是否 清理用户为0的部门
* @return
*/
private static List<Dept> tree(List<Dept> dept, List<User> user, boolean clearZero) {
List<Dept> tree = new ArrayList<>();
// 部门编码与用户集合键值对 deptcode - userList
Map<String, List<User>> deptUserMap = user.stream().collect(Collectors.groupingBy(User::getDeptCode));
// 部门编码与部门键值对 deptcode - dept
Map<String, Dept> deptMap = dept.stream().collect(Collectors.toMap(Dept::getCode, Dept -> Dept));
dept.stream().forEach(d -> {
// 放入用户集合和数量
List<User> deptlist = deptUserMap.get(d.getCode());
if (deptlist != null) {
d.setUserItem(deptlist);
d.setUserNum(deptlist.size());
}
// 获取上级,如无上级则为顶级
Dept parent = deptMap.get(d.getParentCode());
if (parent != null) {
List<Dept> items = parent.getItem();
if (items == null) {
items = new ArrayList<>();
}
items.add(d);
// 数量逐层累计
parent.setUserNum(parent.getUserNum() + d.getUserNum());
parent.setItem(items);
} else {
tree.add(d);
}
});
if (clearZero) {
clearZeroDept(tree);
}
return tree;
}
/**
* 清理用户为0的部门
*
* @param tree
*/
private static void clearZeroDept(List<Dept> tree) {
for (Iterator<Dept> iterator = tree.iterator(); iterator.hasNext();) {
Dept dept = iterator.next();
if (dept.getUserNum() == 0) {
iterator.remove();
}
if (dept.getItem() != null) {
clearZeroDept(dept.getItem());
}
}
}
/**
* 初始化用户
*
* @return
*/
private static List<User> initUser() {
List<User> userList = new ArrayList<>();
User u1 = new User().setCode("2001").setName("张三").setDeptCode("01");
User u2 = new User().setCode("2002").setName("李四").setDeptCode("0201");
User u3 = new User().setCode("2003").setName("王五").setDeptCode("0201");
User u4 = new User().setCode("2004").setName("赵六").setDeptCode("0201");
userList.add(u1);
userList.add(u2);
userList.add(u3);
userList.add(u4);
return userList;
}
/**
* 初始化部门
*
* @return
*/
private static List<Dept> initDept() {
List<Dept> deptList = new ArrayList<>();
Dept sjb = new Dept().setCode("01").setName("设计部").setParentCode("-1");
Dept kfb = new Dept().setCode("02").setName("开发部").setParentCode("-1");
Dept kfb1 = new Dept().setCode("0201").setName("开发组1").setParentCode("02");
Dept kfb2 = new Dept().setCode("0202").setName("开发组2").setParentCode("02");
Dept kfb22 = new Dept().setCode("020201").setName("开发组2-1").setParentCode("0202");
deptList.add(sjb);
deptList.add(kfb);
deptList.add(kfb1);
deptList.add(kfb2);
deptList.add(kfb22);
return deptList;
}
}
待完善详细说明