zoukankan      html  css  js  c++  java
  • 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;
    }
    }
  • 相关阅读:
    ORACLE 11.2.0.4 OCR VOTING DISK 模拟恢复场景
    Oracle Enterprise Linux 6.4 下配置vncserver
    Oracle Enterprise Linux 6.4 下挂载ISCSI 设备
    关于Solaris 的磁盘的分区
    【Google Earth】pro之视频录制
    【爱江山越野跑】ITRA积分认证流程
    android发送邮件
    android手机有多个摄像头,打开其中一个
    Android截图
    Android中的ACCESS_MOCK_LOCATION权限使用Demo
  • 原文地址:https://www.cnblogs.com/xingluzhe/p/6648008.html
Copyright © 2011-2022 走看看