定義一個上下級的類
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
public class TreeEntity<T> implements Serializable {
/**
*
*/
private static final long serialVersionUID = -1303781676719137068L;
/**
*
*/
private T model;
public T getModel() {
return model;
}
public void setModel(T model) {
this.model = model;
}
public List<TreeEntity<T>> getChildren() {
return children;
}
public void setChildren(List<TreeEntity<T>> children) {
if(this.children ==null){
this.children=new ArrayList<TreeEntity<T>>();
}
this.children = children;
}
private List<TreeEntity<T>> children;
}
定義一個生成 樹形的類
package com.qdcan.basecomm.util;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.qdcan.entity.TreeEntity;
/**
* 獲取樹形結構
*
* @author dypeng
*
*/
public class TreeUtil<T> {
private T t;
private String getidMethod;
private String getParentidMethod;
public TreeUtil(String GetidMethod,String GetParentidMethod) {
this.getidMethod = GetidMethod;
this.getParentidMethod=GetParentidMethod;
}
/**
* 向下獲取樹結構
*
* @param channelid
* @return
* @throws Exception
*/
public List<T> createTree(Set<T> setall) throws Exception {
List<T> list = new ArrayList<T>();
Map<Long, Set<T>> listhash = new HashMap<Long, Set<T>>();
if (setall == null || setall.size() == 0) {
return null;
}
for (T item : setall) {
Long parentid = geParentid(item);
if (listhash.containsKey(parentid)) {
listhash.get(parentid).add(item);
} else {
Set<T> t = new HashSet<T>();
t.add(item);
listhash.put(parentid, t);
}
}
Iterator<T> iterator = listhash.get(0l).iterator();
while (iterator.hasNext()) {
T item = iterator.next();
list.add(item);
getlow(item, listhash, list);
}
return list;
}
private void getlow(T parent, Map<Long, Set<T>> list, List<T> getlist) throws Exception {
Long parentid = getid(parent);
if (list.containsKey(parentid)) {
Set<T> lp = list.get(parentid);
for (T item : lp) {
getlist.add(item);
getlow(item, list, getlist);
}
}
return;
}
public List<TreeEntity<T>> createTreeAsTreeEntity(Set<T> setall) throws Exception {
Map<Long, Set<TreeEntity<T>>> listhash = new HashMap<Long, Set<TreeEntity<T>>>();
if (setall == null || setall.size() == 0) {
return null;
}
for (T item : setall) {
Long parentid = geParentid(item);
if (listhash.containsKey(parentid)) {
TreeEntity<T> et =new TreeEntity<T>();
et.setModel(item);
listhash.get(parentid).add(et);
} else {
Set<TreeEntity<T>> t = new HashSet<TreeEntity<T>>();
TreeEntity<T> et =new TreeEntity<T>();
et.setModel(item);
t.add(et);
listhash.put(parentid, t);
}
}
Iterator<TreeEntity<T>> iterator = listhash.get(0l).iterator();
while (iterator.hasNext()) {
TreeEntity<T> entitys = iterator.next();
getlowasTreeEntity(entitys, listhash);
}
return new ArrayList(listhash.get(0l));
}
private void getlowasTreeEntity(TreeEntity<T> parent, Map<Long, Set<TreeEntity<T>>> list) throws Exception {
Long id = getid(parent.getModel());
if (list.containsKey(id)) {
if(list.get(id)!=null){
parent.setChildren(new ArrayList(list.get(id)));
for(TreeEntity<T> t:parent.getChildren()){
getlowasTreeEntity(t,list);
}
}
}
}
/**
* 獲取父級id
*
* @param item
* @return
* @throws Exception
*/
private Long getid(T item) throws Exception {
Method method = item.getClass().getMethod(getidMethod, null);
Object invoke = method.invoke(item, null);
Long id = 0l;
if (invoke != null) {
id = Long.parseLong(invoke.toString());
}
return id;
}
/**
* 獲取父級id
*
* @param item
* @return
* @throws Exception
*/
private Long geParentid(T item) throws Exception {
Method method = item.getClass().getMethod(getParentidMethod, null);
Object invoke = method.invoke(item, null);
Long parentid = 0l;
if (invoke != null) {
parentid = Long.parseLong(invoke.toString());
}
return parentid;
}
}
3.使用方式
private void test1(){
TreeUtil<Menus> treeutil = new TreeUtil<Menus>("getId","getParentid");
Menus m = new Menus();
m.setId(1);
m.setParentid(0);
Menus m1 = new Menus();
m1.setId(2);
m1.setParentid(1);
Menus m2 = new Menus();
m2.setId(3);
m2.setParentid(2);
List<Menus> setall = Arrays.asList(new Menus[]{m,m1,m2});
try {
List<TreeEntity<Menus>> menusTree = treeutil.createTreeAsTreeEntity(new HashSet<Menus>(setall)) ;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}