list转tree java泛型工具

list转tree泛型工具,在 https://blog.csdn.net/massivestars/article/details/53911620/ 基础上泛型化,代码如下

package cn.sky.framework.util.tree;

import java.util.List;

/**
 * 树节点
 * 
 * @author gaofu
 *
 * @param 
 */
public abstract class TreeNode> {

	/**
	 * 判断是否是主枝干
	 * 
	 * @return
	 */
	public boolean isTrunk() {
		return getParentId() == null;
	}

	/**
	 * 判断本节点是否是node的父节点
	 * 
	 * @param node
	 * @return
	 */
	public boolean isParentOf(T node) {
		if (node != null) {
			return TreeUtil.isEquals(node.getParentId(), this.getId());
		}
		return false;
	}

	/**
	 * 判断本节点是否是node的孩子节点
	 * 
	 * @param node
	 * @return
	 */
	public boolean isChildOf(T node) {
		if (node == null) {
			if (this.getParentId() == null) {
				return true;
			}
		} else {
			return TreeUtil.isEquals(node.getId(), this.getParentId());
		}
		return false;
	}

	public abstract String getParentId();

	public abstract void setParentId(String parentId);

	public abstract String getId();

	public abstract void setId(String id);

	public abstract List getChild();

	public abstract void setChild(List child);

}
package cn.sky.framework.util.tree;

import java.util.ArrayList;
import java.util.List;

public class TreeUtil {

	/**
	 * 列表转换为树
	 * 
	 * @param nodeList
	 * @return
	 */
	public static > List listToTree(List nodeList) {
		List tree = new ArrayList();
		for (T node : nodeList) {
			if (node.isTrunk()) {
				tree.add(findChildren(node, nodeList));
			}
		}
		return tree;
	}

	/**
	 * 递归查找子节点
	 * 
	 * @param nodeList
	 * @return
	 */
	public static > T findChildren(T node, List nodeList) {
		for (T it : nodeList) {
			if (node.isParentOf(it)) {
				if (node.getChild() == null) {
					node.setChild(new ArrayList());
				}
				node.getChild().add(findChildren(it, nodeList));
			}
		}
		return node;
	}

	/**
	 * 两个对象是否相等
	 * 
	 * @param source
	 * @param target
	 * @return
	 */
	public static boolean isEquals(Object source, Object target) {
		if (source == null && target == null) {
			return true;
		}
		if (source == null || target == null) {
			return false;
		}
		return source.equals(target);
	}

}
package cn.sky.test;

import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

import com.google.gson.Gson;

import cn.sky.framework.util.tree.TreeNode;
import cn.sky.framework.util.tree.TreeUtil;

public class TreeTest {

	static class Menu extends TreeNode {
		String id;
		String parentId;
		String name;
		List child;

		public Menu(String id, String name, String parentId) {
			this.id = id;
			this.name = name;
			this.parentId = parentId;
		}

		@Override
		public String getParentId() {
			return parentId;
		}

		@Override
		public void setParentId(String parentId) {
			this.parentId = parentId;
		}

		@Override
		public String getId() {
			return id;
		}

		@Override
		public void setId(String id) {
			this.id = id;
		}

		@Override
		public List getChild() {
			return this.child;
		}

		@Override
		public void setChild(List child) {
			this.child = child;
		}

	}

	/**
	 * 测试用例
	 */
	@Test
	public void testCode() {
		Menu treeNode1 = new Menu("1", "广州", null);
		Menu treeNode2 = new Menu("2", "深圳", null);

		Menu treeNode3 = new Menu("3", "天河区", treeNode1.getId());
		Menu treeNode4 = new Menu("4", "越秀区", treeNode1.getId());
		Menu treeNode5 = new Menu("5", "黄埔区", treeNode1.getId());
		Menu treeNode6 = new Menu("6", "石牌", treeNode3.getId());
		Menu treeNode7 = new Menu("7", "百脑汇", treeNode6.getId());

		Menu treeNode8 = new Menu("8", "南山区", treeNode2.getId());
		Menu treeNode9 = new Menu("9", "宝安区", treeNode2.getId());
		Menu treeNode10 = new Menu("10", "科技园", treeNode8.getId());

		List list = new ArrayList();
		list.add(treeNode1);
		list.add(treeNode2);
		list.add(treeNode3);
		list.add(treeNode4);
		list.add(treeNode5);
		list.add(treeNode6);
		list.add(treeNode7);
		list.add(treeNode8);
		list.add(treeNode9);
		list.add(treeNode10);

		List trees = TreeUtil.listToTree(list);
		System.out.println(new Gson().toJson(trees));

	}

}

 

你可能感兴趣的:(java)