根据子节点递归查找所有父节点ID

it2024-11-05  6

递归获取所有父节点测试用例: 参数说明: id为当前节点id; list为所有节点对像

@Override public List<String> queryParentIds(String id, List<TaxCompany> taxCompanyList) { //递归获取父级ids,不包含自己 List<String> parentIds = new ArrayList<>(); this.getParentTaxCompanyIds(taxCompanyList, id, parentIds); return parentIds; } /** * 递归获取父级ids * @param taxCompanyList * @param id * @param taxCompanyIds */ private void getParentTaxCompanyIds(List<TaxCompany> taxCompanyList, String id, List<String> parentIds) { for (TaxCompany taxCompany : taxCompanyList) { if (StringUtils.isEmpty(taxCompany.getcParentid())) { continue; } //判断是否有父节点 if (id.equals(taxCompany.getcCompanyid())) { taxCompanyIds.add(taxCompany.getcParentid()); getParentTaxCompanyIds(taxCompanyList, taxCompany.getcParentid(), parentIds); } } }
最新回复(0)