import java.util.HashSet; import java.util.List; import java.util.Set; import org.junit.Test; import com.smc.webapp.domain.dispatch.CatalogVO; public class CatalogTest { @Test public void getCatalogAllChildIdTest(){ CatalogVO t0 = new CatalogVO(); t0.setId("0"); CatalogVO t1 = new CatalogVO(); t1.setId("1"); CatalogVO t2 = new CatalogVO(); t2.setId("2"); CatalogVO t3 = new CatalogVO(); t3.setId("3"); CatalogVO t11 = new CatalogVO(); t11.setId("11"); CatalogVO t22 = new CatalogVO(); t22.setId("22"); CatalogVO t33 = new CatalogVO(); t33.setId("33"); CatalogVO t333 = new CatalogVO(); t333.setId("333"); t0.setParent_id(""); t1.setParent_id("0"); t2.setParent_id("0"); t3.setParent_id("0"); t11.setParent_id("1"); t22.setParent_id("2"); t33.setParent_id("3"); Set<CatalogVO> t0Childern = new HashSet<CatalogVO>(); t0Childern.add(t1); t0Childern.add(t2); t0Childern.add(t3); t0.setChildren(t0Childern); Set<CatalogVO> t1Childern = new HashSet<CatalogVO>(); t1Childern.add(t11); t1.setChildren(t1Childern); Set<CatalogVO> t2Childern = new HashSet<CatalogVO>(); t2Childern.add(t22); t2.setChildren(t2Childern); Set<CatalogVO> t3Childern = new HashSet<CatalogVO>(); t3Childern.add(t33); t3.setChildren(t3Childern); String info = this.iteratorTree(t0); System.out.println(info); } public String iteratorTree(CatalogVO cCatalogVO){ StringBuilder allNodeInfo = new StringBuilder(); if(cCatalogVO != null){ if("".equals(cCatalogVO.getParent_id())){//根节点 allNodeInfo.append(cCatalogVO.getId() + ","); } for(CatalogVO tCatalogVO : cCatalogVO.getChildren()){ allNodeInfo.append(tCatalogVO.getId() + ","); if(tCatalogVO.getChildren() != null && tCatalogVO.getChildren().size() > 0){ allNodeInfo.append(iteratorTree(tCatalogVO)); } } } return allNodeInfo.toString(); } }