• java递归生成树结构的数据


    @Data
    @EqualsAndHashCode(callSuper =true)
    @ApiModel(value = "AccountCaptionVo", description = "会计科目")
    public class AccountCaptionVo extends BaseModel {

    @ApiModelProperty(value ="会计科目名称",name = "captionName")
    private String captionName;

    @ApiModelProperty(value = "会计科目编码",name = "captionCode")
    private String captionCode;

    @ApiModelProperty(value = "父类编码",name = "parentId")
    private Long parentId;

    @ApiModelProperty(value = "系统科目",name = "systematicSubjects")
    private String systematicSubjects;

    @ApiModelProperty(value = "借贷方向",name = "lendingDirection")
    private String lendingDirection;

    @ApiModelProperty(value = "子集",name = "children")
    private List<AccountCaptionVo> children;

    @ApiModelProperty(value = "科目名称助记码",name = "mnemonicCode")
    private String mnemonicCode;



    public static List<AccountCaptionVo> listToTree(List<AccountCaptionVo> list) {
    //用递归找子。
    List<AccountCaptionVo> treeList = new ArrayList<AccountCaptionVo>();
    for (AccountCaptionVo tree : list) {
    //根目录的parentId为-1
    if (tree.getParentId() == -1 ) {
    treeList.add(findChildren(tree, list));
    }
    }
    return treeList;
    }

    private static AccountCaptionVo findChildren(AccountCaptionVo tree, List<AccountCaptionVo> list) {
    for (AccountCaptionVo node : list) {
    if (node.getParentId().longValue() == tree.getId().longValue()) {
    if (tree.getChildren() == null) {
    tree.setChildren(new ArrayList<AccountCaptionVo>());
    }
    tree.getChildren().add(findChildren(node, list));
    }
    }
    return tree;
    }

    @GetMapping(path="")
    @ApiOperation(value="获取所有会计科目",nickname="getAccountCaption")
    public Iterable<AccountCaptionVo> List(){
    List<AccountCaption> listAccountCaption= capAccountRepository.selecttSql();
    List<AccountCaptionVo> listAccountCaptionVos=new ArrayList<>();
    listAccountCaption.stream().forEach(x->
    {
    AccountCaptionVo AccountCaptionVo=new AccountCaptionVo();
    try {
    BeanUtils.copyProperties(AccountCaptionVo,x);
    listAccountCaptionVos.add(AccountCaptionVo);
    } catch (IllegalAccessException e) {
    e.printStackTrace();
    } catch (InvocationTargetException e) {
    e.printStackTrace();
    }
    });
    return listToTree(listAccountCaptionVos);
    }
  • 相关阅读:
    Docker-Compose搭建单体SkyWalking 6.2
    Docker搭建MySQL主从集群,基于GTID
    【简记】修改Docker数据目录位置,包含镜像位置
    【拆分版】Docker-compose构建Kibana单实例,基于7.1.0
    【拆分版】Docker-compose构建Logstash多实例,基于7.1.0
    【拆分版】Docker-compose构建Zookeeper集群管理Kafka集群
    命令行模式和python交互模式
    详解 HTTPS、TLS、SSL、HTTP区别和关系
    windows下如何查看进程、端口占用、杀死进程教程
    pycharm最常用的快捷键总结
  • 原文地址:https://www.cnblogs.com/gemiaomiao/p/12160521.html
Copyright © 2020-2023  润新知