Java教程

Java递归树形结构

本文主要是介绍Java递归树形结构,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
private List<ZcprojectFieldDto> buildGeneralTree(List<ZcprojectFieldDto> list) {         List<ZcprojectFieldDto> result = new ArrayList<>();         //1. 构建一级节点         for (ZcprojectFieldDto zcprojectFieldDto : list) {             if (zcprojectFieldDto.getPId().equals("0")) {                 result.add(zcprojectFieldDto);             }         }           // 2、递归获取子节点         for (ZcprojectFieldDto parent : result) {             parent = recursiveTree(parent, list);         }         return result;     }       /**      * 递归      *      * @return      */     private ZcprojectFieldDto recursiveTree(ZcprojectFieldDto parent, List<ZcprojectFieldDto> list) {         for (ZcprojectFieldDto zcprojectFieldDto : list) {             if (Objects.equals(parent.getId(), zcprojectFieldDto.getPId())) {                 zcprojectFieldDto = recursiveTree(zcprojectFieldDto, list);                 parent.getChildren().add(zcprojectFieldDto);             }         }         return parent;     }
这篇关于Java递归树形结构的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!