zoukankan      html  css  js  c++  java
  • java 8 递归 并且根据某个字段排序

    1.实体bean结构
    public class DmpDictVo {
    @ApiModelProperty(value = "数据id")
    private String id;

    @ApiModelProperty(value = "父id")
    private String pid;

    @ApiModelProperty(value = "描述")
    private String name;

      //初始化 children 避免报空指针异常
        List<DmpDictVo> children = new ArrayList<>();
    }


    // 根据自己的业务编写
    private List<DmpDictVo> searchFatherDmpDict() {
     // 查询出所有的数据字典
    List<QaCategoryVo> qaCategoryVoList = findAll(type);
    // 递归封装数据

    return makeTree(qaCategoryVoList, -1L);
    
    
    }


    /**
    * 递归方法构造
    */

    private List<QaCategoryVo> makeTree(List<QaCategoryVo> departmentList, Long pId) {

    //子类
    List<QaCategoryVo> children = departmentList.stream().filter(x -> x.getPid().equals(pId)).collect(Collectors.toList());
    // 排序
    children = children.stream().sorted(Comparator.comparing(QaCategoryVo::getSort, Comparator.nullsLast(Integer::compareTo)).reversed()).collect(Collectors.toList());
      
    if(children.isEmpty()){
    return null;
    }
    //后辈中的非子类
    List<QaCategoryVo> successor = departmentList.stream().filter(x -> !x.getPid().equals(pId)).collect(Collectors.toList());

    for (QaCategoryVo x : children) {
    List<QaCategoryVo> qaCategoryVos = makeTree(successor, x.getId());
    x.setChildren(qaCategoryVos);
    }
    return children;

    }
  • 相关阅读:
    【codeforces 510D】Fox And Jumping
    【codeforces 755E】PolandBall and White-Red graph
    实用SQL语句大全
    经典SQL语句大全
    mysql安装及使用语句
    ubuntu安装mysql数据库
    android数据库sqlite增加删改查
    ubuntu 15.04怎么安装QQ
    Tagging Physical Resources in a Cloud Computing Environment
    程序员的10大编程技巧
  • 原文地址:https://www.cnblogs.com/bt2882/p/13086701.html
Copyright © 2011-2022 走看看