java树形结构泛型

定义一个上下级的类

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.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.qdcan.entity.TreeEntity;

/**
 * 获取树形结构
 * 
 * @author dypeng
 *
 */
public class TreeUtil<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 = getParentid(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 LinkedHashMap<Long, Set<TreeEntity<T>>>();
		if (setall == null || setall.size() == 0) {
			return null;
		}

		for (T item : setall) {
			Long parentid = getParentid(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 LinkedHashSet<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 getParentid(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();
		}
	}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值