java 遍历tree_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > java 遍历tree

java 遍历tree

 2012/1/18 9:25:50  elan1986  程序员俱乐部  我要评论(0)
  • 摘要:importjava.util.HashSet;importjava.util.List;importjava.util.Set;importorg.junit.Test;importcom.smc.webapp.domain.dispatch.CatalogVO;publicclassCatalogTest{@TestpublicvoidgetCatalogAllChildIdTest(){CatalogVOt0=newCatalogVO();t0.setId("0")
  • 标签:遍历 Java
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();
	}
}
发表评论
用户名: 匿名