递归获取所有父节点测试用例: 参数说明: 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); } } }