`
JavaSam
  • 浏览: 934042 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

将数据库树形记录转为java 树形数据结构

    博客分类:
  • JAVA
 
阅读更多
    /**
	 * 转换为Tree结构
	 * @param treedata
	 * @return
	 */
	public static Tree toTreeList(List<Tree> treedata){
		Tree root = null;
		for(Tree node:treedata){
			String parentid = node.getParentid();
			if(parentid.equals("0"))
				root = node;
		    Tree parent = getTarget(treedata,parentid); 
		    if(parent != null){
		    	List<Tree> children = parent.getChildren();
		    	if(children == null){
		    		children = new ArrayList<Tree>();
		    		parent.setChildren(children);
		    	}
		        children.add(node);
		    }
		}
		return root;
	}
	/**
	 * 获得指定节点
	 * @param treedata
	 * @param id
	 * @return
	 */
	private static Tree getTarget(List<Tree> treedata,String id){
		for(Tree node:treedata){
			String _id = node.getId();
			if(_id.equals(id))
				return node;
		}
		return null;
	}

 

 

1
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics