不用递归实现生成树的Example
这是用于生成树的类,适用于分类号采用的是自增的方式,并且List中的元素是按从小到大的顺序进行过排序的(这个在查询时很容易实现,加上order by [id] 即可)。
这样只需要进行一次数据库查询操作,如果用递归会查询N多次。
package com.zhenjw.tree;import java.util.List;import java.util.Vector;public class Tree { public static List createTree(List<TreeNode> all) {if (all == null || all.size() == 0) return null;int count = all.size();TreeNode bi = new TreeNode();TreeNode bj = new TreeNode();//先组织好父子之间的关系然后 填充layer与parentname这两个字段的内容//1、组织好父子之间的关系for (int i = 0; i < count; i++) { bi = (TreeNode) all.get(i); bi.setLayer(0); bi.setFullName(bi.getName()); boolean trans = false; for (int j = i + 1; j < count; j++) {bj = (TreeNode) all.get(j);if (bi.getParentNo() == bj.getNo()) { //是父子关系 all.set(i, bj); all.set(j, bi); //父在前面子在后面 trans = true;} } if (trans)i--;}//2、组织各个元素所在的层以及全称for (int i = 0; i < count; i++) { bi = (TreeNode) all.get(i); for (int j = i + 1; j < count; j++) {bj = (TreeNode) all.get(j);if (bj.getParentNo() == bi.getNo()) { bj.setLayer(bi.getLayer() + 1); bj.setFullName(bi.getFullName() + ">>" + bj.getName());} }}return all; } public static List createTree(List all, int topid) {all = createTree(all);int count = all.size();TreeNode bj = new TreeNode();//把不在这一顶层的其它项都删除掉int topidj = 0;int topidlayer = 0;//查找ID=topid所在的位置for (int j = 0; j < count; j++) { bj = (TreeNode) all.get(j); //找到TopID对应的对象 if (bj.getNo() == topid) {topidj = j;topidlayer = bj.getLayer();break; }}List reslut = new Vector();for (int j = topidj; j < count; j++) { bj = (TreeNode) all.get(j); //找到TopID对应的对象 if (bj.getLayer() <= topidlayer && j > topidj) {break; } else {bj.setLayer(bj.getLayer() - topidlayer);reslut.add(bj); }}return reslut; }}package com.zhenjw.tree;import org.apache.commons.lang.builder.ToStringBuilder;public class TreeNode<T> { private long no; private long parentNo; private String name; private String fullName; private int layer; //用于保存原始的对象,方便在列表时得到对象的其它的信息 private T entity; public T getEntity() {return entity; } public void setEntity(T entity) {this.entity = entity; } public String getFullName() {return fullName; } public void setFullName(String fullName) {this.fullName = fullName; } public String getName() {return name; } public void setName(String name) {this.name = name; } public long getNo() {return no; } public void setNo(long no) {this.no = no; } public long getParentNo() {return parentNo; } public void setParentNo(long parentNo) {this.parentNo = parentNo; } public TreeNode(long no, long parentNo, String name, String fullName, int layer, T entity) {super();this.no = no;this.parentNo = parentNo;this.name = name;this.fullName = fullName;this.layer = layer;this.entity = entity; } public TreeNode(long no, long parentNo, String name, T entity) {super();this.no = no;this.parentNo = parentNo;this.name = name;this.entity = entity; } public TreeNode() {super();// TODO Auto-generated constructor stub } public int getLayer() {return layer; } public void setLayer(int layer) {this.layer = layer; } public String toString() {return ToStringBuilder.reflectionToString(this); }}