java list转换为树形

unix 发布于 2015/05/08 20:32
阅读 10K+
收藏 3

我想要将下面的一个list转换成一个树形结构,但是有点问题,帮忙看一下怎么修改。代码如下:

public class TestTree {

	public static void main(String[] args) {
		
		Bean up1= getBean(11806,1,11802);
		Bean up2= getBean(11797,1,11806);
		Bean up3= getBean(11798,1,11796);
		Bean up4= getBean(11799,1,11804);
		Bean up5= getBean(11796,1,0);
		Bean up6= getBean(11800,2,0);
		Bean up7= getBean(11801,2,11802);
		Bean up8= getBean(11804,2,11796);
		Bean up9= getBean(11803,2,11804);
		Bean up10= getBean(11802,3,0);
		Bean up11= getBean(11805,4,0);
		
		List<Bean> list = new ArrayList<Bean>();
		list.add(up1);
		list.add(up2);
		list.add(up3);
		list.add(up4);
		list.add(up5);
		list.add(up6);
		list.add(up7);
		list.add(up8);
		list.add(up9);
		list.add(up10);
		list.add(up11);
		
		List<Bean> result = BeanSort.sort(list);
		for (Bean bean : result) {
			System.out.println(bean);
		}
	}
	
	private static Bean getBean(int id, int sortId, int parentId) {
		Bean bean = new Bean();
		bean.setId(id);
		bean.setSortId(sortId);
		bean.setParentId(parentId);
		
		return bean;
	}
}
public class Bean {
	private int id;
	private int sortId;
	private int parentId;
	private List<Bean> children = new ArrayList<Bean>();
	//省略get/set
}
public class BeanSort {
	
	public static List<Bean> sort(List<Bean> beans) {
		
		List<Bean> result = new ArrayList<Bean>();
		
		for (Bean bean : beans) {
			if (bean.getParentId() == 0) {
				result.add(bean);
				sort(bean.getId(), beans, result.get(result.size() - 1), result);
			}
		}
		
		return result;
	}
	
	private static void sort(long id, List<Bean> beans, Bean target, List<Bean> result) {
		for (Bean bean : beans) {
			if (bean.getParentId() == id) {
				target.getChildren().add(bean);
			} else {
				//这里怎么获取更下一级的?
			}
		}
	}
}



加载中
0
nightmare123
nightmare123
使用双层for循环就可以轻松解决问题啦!
u
unix
确实可以,思路进入死角了。谢谢。
0
nightmare123
nightmare123
import java.util.ArrayList;
import java.util.List;


public class Tree {
	private int id;
	
	private String name;
	
	private int parentId;
	
	private List<Tree> childrens;

	public Tree(int id,String name,int parentId){
		this.id = id;
		this.name = name;
		this.parentId = parentId;
	}
	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getParentId() {
		return parentId;
	}

	public void setParentId(int parentId) {
		this.parentId = parentId;
	}

	public List<Tree> getChildrens() {
		return childrens;
	}

	public void setChildrens(List<Tree> childrens) {
		this.childrens = childrens;
	}
	
	
	
	@Override
	public String toString() {
		return "Tree [id=" + id + ", name=" + name + ", parentId=" + parentId
				+ ", childrens=" + childrens + "]";
	}

	public static void main(String[] args) {
		Tree tree1 = new Tree(1, "顶层节点1", 0);
		Tree tree2 = new Tree(2, "顶层节点2", 0);
		Tree tree3 = new Tree(3, "顶层节点3", 0);
		
		Tree tree4 = new Tree(4, "二级节点4", 1);
		Tree tree5 = new Tree(5, "二级节点5", 2);
		Tree tree6 = new Tree(6, "二级节点6", 3);
		
		Tree tree7 = new Tree(7, "三级节点7", 4);
		Tree tree8 = new Tree(8, "三级节点8", 4);
		Tree tree9 = new Tree(9, "三级节点9", 5);
		
		List<Tree> trees = new ArrayList<Tree>();
		trees.add(tree9);
		trees.add(tree8);
		trees.add(tree7);
		trees.add(tree6);
		trees.add(tree5);
		trees.add(tree4);
		trees.add(tree3);
		trees.add(tree2);
		trees.add(tree1);

		List<Tree> rootTrees = new ArrayList<Tree>();
		for (Tree tree : trees) {
			if(tree.getParentId() == 0){
				rootTrees.add(tree);
			}
			for (Tree t : trees) {
				if(t.getParentId() == tree.getId()){
					if(tree.getChildrens() == null){
						List<Tree> myChildrens = new ArrayList<Tree>();
						myChildrens.add(t);
						tree.setChildrens(myChildrens);
					}else{
						tree.getChildrens().add(t);
					}
				}
			}
		}
		
		for (Tree tree : rootTrees) {
			System.out.println(tree.toString());
		}
		
	}

}



0
0
alexgaoyh
alexgaoyh
形如如下的递归

http://git.oschina.net/alexgaoyh/alexgaoyh/blob/master/src/main/java/com/alexgaoyh/admin/login/action/AdminController.java

private List<TreeNode> resourceToTreeNode(List<SysmanResource> resource) {

		//Use isEmpty() to check whether the collection is empty or not.
		if (resource != null && !resource.isEmpty() && resource.get(0).getResourceType() == SysmanResource.TYPE_MENU) {
			List<TreeNode> ch = new ArrayList<TreeNode>();
			for (SysmanResource rr : resource) {
				
				TreeNode node = new TreeNode();
				
				if(rr.getHref()==null){
					node.setId(rr.getPid());
				}else{
					node.setId(rr.getPid());
				}
				
				node.setState("open");
				node.setText(rr.getName());
				
				Map<String, Object> _temp = new HashMap<String, Object>();
				_temp.put("href", rr.getHref());
				node.setAttributes(_temp);
				
				ch.add(node);
				node.setChildren(resourceToTreeNode(rr.getSubResource()));
			}

			return ch;
		}
		//Empty arrays and collections should be returned instead of null
		return Collections.emptyList();
	}



返回顶部
顶部