jdk8递归生成节点树代码

测试用实体类:Role.java

package com.demo;

import java.util.List;

public class Role {

  private Long id;
  private Long pid;
  private String roleName;
  private List<Role> childerns;

  public Long getId() {
    return id;
  }

  public void setId(Long id) {
    this.id = id;
  }

  public Long getPid() {
    return pid;
  }

  public void setPid(Long pid) {
    this.pid = pid;
  }

  public String getRoleName() {
    return roleName;
  }

  public void setRoleName(String roleName) {
    this.roleName = roleName;
  }

  public List<Role> getChilderns() {
    return childerns;
  }

  public void setChilderns(List<Role> childerns) {
    this.childerns = childerns;
  }

  @Override
  public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((id == null) ? 0 : id.hashCode());
    return result;
  }

  @Override
  public boolean equals(Object obj) {
    if (this == obj)
      return true;
    if (obj == null)
      return false;
    if (getClass() != obj.getClass())
      return false;
    Role other = (Role) obj;
    if (id == null) {
      if (other.id != null)
        return false;
    } else if (!id.equals(other.id))
      return false;
    return true;
  }

  @Override
  public String toString() {
    return "Role [id=" + id + ", pid=" + pid + ", roleName=" + roleName + ", childerns=" + childerns + "]";
  }


}

测试数据是,两个*角色,总经理 和 总管理者, 总经理 --> 地区经理 和 分区经理 ; 总管理者 --> 高级管理者 --> 中级管理者 --> 低级管理者
使用jdk8的新特性,不使用递归完成节点代码:

package com.daxiong.test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.alibaba.fastjson.JSONObject;

public class JavaRootTreeTest {

  public static void main(String[] args) {
    JavaRootTreeTest t = new JavaRootTreeTest();
    List<Role> roles = t.findAllRoles();
    System.out.println(roles);
    List<Role> root = t.getRootTree(roles);
    System.out.println(JSONObject.toJSONString(root));
  }

  public List<Role> getRootTree(List<Role> roles) {
    if (roles == null || roles.size() == 0) {
      return Collections.emptyList();
    }
    Map<Long, Role> idMap = roles.parallelStream().collect(Collectors.toMap(Role::getId, p -> p));
    // System.out.println(idMap);
    Map<Long, List<Role>> pidGroupRolesMap = roles.parallelStream().collect(Collectors.groupingBy(Role::getPid));
    System.out.println(pidGroupRolesMap);
    pidGroupRolesMap.forEach((k, v) -> {
      Role parentRole = idMap.get(k);
      if (parentRole != null) {
        parentRole.setChilderns(v);
      }
    });
    System.out.println(pidGroupRolesMap);
    return pidGroupRolesMap.get(0L);
  }

  public List<Role> findAllRoles() {
    List<Role> roles = new ArrayList<Role>();
    Role role1 = new Role();
    role1.setId(1L);
    role1.setPid(0L);
    role1.setRoleName("总管理者");
    roles.add(role1);
    Role role2 = new Role();
    role2.setId(2L);
    role2.setPid(1L);
    role2.setRoleName("高级管理者");
    roles.add(role2);
    Role role3 = new Role();
    role3.setId(3L);
    role3.setPid(2L);
    role3.setRoleName("中级管理者");
    roles.add(role3);
    Role role4 = new Role();
    role4.setId(4L);
    role4.setPid(3L);
    role4.setRoleName("低级管理者");
    roles.add(role4);
    Role role5 = new Role();
    role5.setId(5L);
    role5.setPid(0L);
    role5.setRoleName("总经理");
    roles.add(role5);
    Role role6 = new Role();
    role6.setId(6L);
    role6.setPid(5L);
    role6.setRoleName("地区经理");
    roles.add(role6);
    Role role7 = new Role();
    role7.setId(7L);
    role7.setPid(5L);
    role7.setRoleName("分区经理");
    roles.add(role7);
    return roles;
  }
}

测试数据是,两个*角色,总经理 和 总管理者, 总经理 --> 地区经理 和 分区经理 ; 总管理者 --> 高级管理者 --> 中级管理者 --> 低级管理者
运行结果如下:

[
	{
		"childerns": [
			{
				"childerns": [
					{
						"childerns": [
							{
								"id": 4,
								"pid": 3,
								"roleName": "低级管理者"
							}
						],
						"id": 3,
						"pid": 2,
						"roleName": "中级管理者"
					}
				],
				"id": 2,
				"pid": 1,
				"roleName": "高级管理者"
			}
		],
		"id": 1,
		"pid": 0,
		"roleName": "总管理者"
	},
	{
		"childerns": [
			{
				"id": 6,
				"pid": 5,
				"roleName": "地区经理"
			},
			{
				"id": 7,
				"pid": 5,
				"roleName": "分区经理"
			}
		],
		"id": 5,
		"pid": 0,
		"roleName": "总经理"
	}
]
jdk8递归生成节点树代码jdk8递归生成节点树代码 qq_42958716 发布了2 篇原创文章 · 获赞 0 · 访问量 21 私信 关注
上一篇:【题解】猜数


下一篇:ansible通过角色功能roles实现nfs部署