• java8 创建树结构的数据


    private List<TreeNode> createTree(Integer pid, Map<Integer, List<SysPermission>> map){
    return Optional.ofNullable(map.get(pid)).orElseGet(()->new ArrayList<SysPermission>()).stream().filter(x->x.getParentId() == pid).sorted((x,y)->{return x.getSortNo().compareTo(y.getSortNo());}).map(x->{
    return new TreeNode(x.getRecId(), x.getParentId(), x.getPermissionName(), x.getIcon(), createTree(x.getRecId(), map));
    }).collect(Collectors.toList());
    }

    private List<TreeNode> getTree(boolean onlyModule){
    EntityWrapper<SysPermission> entityWrapper = new EntityWrapper<>();
    entityWrapper.where("data_status>0");
    if (onlyModule){
    entityWrapper.eq("permission_type", PermissionType.MODULE.getValue());
    }
    entityWrapper.orderBy("sort_no");
    List<SysPermission> list = selectList(entityWrapper);
    return createTree(0, list.stream().collect(groupingBy(SysPermission::getParentId)));
    }

    TreeNode的定义:
    public class TreeNode {
    private static final long serialVersionUID = 1L;

    private Integer recId;
    private Integer pid;
    private String nodeName;
    private String icon;
    private List<TreeNode> children;

    public TreeNode(Integer recId, Integer pid, String nodeName, String icon) {
    this.recId = recId;
    this.pid = pid;
    this.nodeName = nodeName;
    this.icon = icon;
    this.children = new ArrayList<TreeNode>();
    }

    public TreeNode(Integer recId, Integer pid, String nodeName, String icon, List<TreeNode> children) {
    this.recId = recId;
    this.pid = pid;
    this.nodeName = nodeName;
    this.icon = icon;
    this.children = children;
    }

    public Integer getRecId() {
    return recId;
    }

    public void setRecId(Integer recId) {
    this.recId = recId;
    }

    public Integer getPid() {
    return pid;
    }

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

    public String getNodeName() {
    return nodeName;
    }

    public void setNodeName(String nodeName) {
    this.nodeName = nodeName;
    }

    public String getIcon() {
    return icon;
    }

    public void setIcon(String icon) {
    this.icon = icon;
    }

    public List<TreeNode> getChildren() {
    return children;
    }

    public void setChildren(List<TreeNode> children) {
    this.children = children;
    }
    }
  • 相关阅读:
    Java异常的分类
    Java SE 6 新特性: Java DB 和 JDBC 4.0
    Java SE 6 新特性: 对脚本语言的支持
    面向对象开发方法优点
    RocketMQ之八:水平扩展及负载均衡详解
    Bluetooth 4.0之Android 解说
    iOS截取视频缩略图的两种方法
    Java NIO Buffer
    spark 启动job的流程分析
    C语言堆内存管理上出现的问题,内存泄露,野指针使用,非法释放指针
  • 原文地址:https://www.cnblogs.com/xingluzhe/p/6648008.html
Copyright © 2020-2023  润新知