将无序的List数据转成树形结构数据

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/Joker_2007/article/details/82894857

将无序的List数据转成树形结构数据

在平常的项目中,经常会用到树形结构数据,看了一些网友写的生成树形结构的方法,感觉基本都会把层级写的比较死,所以在这里写一个不关联层级的生成方式(层级信息在生成树的时候赋值),依靠code和parentCode两个信息生成最终的树。

Tree.java

package patterns.structure.composite;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用组合模式构建一棵树
 * 
 * @author joker
 * @date 2018-09-27 22:47
 */
public class Tree {

	private String code;

	private String name;

	private String parentCode;

	private Integer level;

	private Tree parent;

	private List<Tree> node;

	public Tree() {
	}

	public Tree(String code, String name, String parentCode, Integer level) {
		this.code = code;
		this.name = name;
		this.parentCode = parentCode;
		this.level = level;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	/**
	 * 将一组无序的树组装成树形结构的数据
	 * 
	 * @param treeList
	 * @return
	 */
	public static List<Tree> buildTree(List<Tree> treeList) {
		if (treeList == null || treeList.size() == 0) {
			return null;
		}
		List<Tree> trees = getRootNode(treeList);
		trees.forEach(node -> node.setLevel(0));
		List<Tree> parentNodes = trees;
		List<Tree> subNodes;
		int level = 1;
		do {
			treeList.removeAll(parentNodes);
			subNodes = getRootNode(treeList);
			if (subNodes != null && subNodes.size() > 0) {
				for (Tree subNode : subNodes) {
					subNode.setLevel(level);
					for (Tree parentNode : parentNodes) {
						if (codeEquals(parentNode.getCode(), subNode.getParentCode())) {
							List<Tree> list = parentNode.getNode();
							if (list == null) {
								list = new ArrayList<>();
								parentNode.setNode(list);
							}
							list.add(subNode);
							subNode.setParent(parentNode);
						}
					}
				}
			}
			parentNodes = subNodes;
			level++;
		} while (subNodes != null);
		return trees;
	}

	/**
	 * 获取treeList中的顶级节点
	 * 
	 * @param treeList
	 * @return
	 */
	public static List<Tree> getRootNode(List<Tree> treeList) {
		if (treeList == null || treeList.size() == 0) {
			return null;
		}
		List<String> codes = treeList.stream().map(tree -> tree.getCode()).collect(Collectors.toList());
		List<Tree> trees = treeList.stream().filter(
				node -> !codes.contains(node.getParentCode()) || codeEquals(node.getCode(), node.getParentCode()))
				.collect(Collectors.toList());
		return trees;
	}

	/**
	 * 判断两个编码是否相同
	 * 
	 * @param code
	 * @param parentCode
	 * @return
	 */
	public static boolean codeEquals(String code, String parentCode) {
		if (code == null) {
			if (parentCode == null) {
				return true;
			}
		} else {
			if (parentCode != null && code.equals(parentCode)) {
				return true;
			}
		}
		return false;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getParentCode() {
		return parentCode;
	}

	public void setParentCode(String parentCode) {
		this.parentCode = parentCode;
	}

	public Integer getLevel() {
		return level;
	}

	public void setLevel(Integer level) {
		this.level = level;
	}

	public Tree getParent() {
		return parent;
	}

	public void setParent(Tree parent) {
		this.parent = parent;
	}

	public List<Tree> getNode() {
		return node;
	}

	public void setNode(List<Tree> node) {
		this.node = node;
	}

	@Override
	public String toString() {
		return "Tree{" + "code='" + code + '\'' + ", name='" + name + '\'' + ", parentCode='" + parentCode + '\''
				+ ", level=" + level + '}';
	}

}

TreeTest.java

package patterns.structure.composite;

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

/**
 * @author joker
 * @date 2018-09-28 22:20
 */
public class TreeTest {

    public static void main(String[] args) {
        Tree tree0 = new Tree(null,"根节点",null,null);
        Tree tree1 = new Tree("1","根节点",null,null);
        Tree tree2 = new Tree("2","根节点",null,null);
        Tree tree3 = new Tree("3","根节点","2",null);
        Tree tree4 = new Tree("4","根节点","2",null);
        Tree tree5 = new Tree("5","根节点","0",null);
        Tree tree6 = new Tree("6","根节点","0",null);
        Tree tree7 = new Tree("7","根节点","6",null);
        Tree tree8 = new Tree("8","根节点","7",null);
        Tree tree9 = new Tree("9","根节点","5",null);
        Tree tree10 = new Tree("10","根节点","5",null);
        Tree tree11 = new Tree("11","根节点","13",null);
        Tree tree12 = new Tree("12","根节点","13",null);
        Tree tree13 = new Tree("13","根节点","10",null);

        List<Tree> treeList = new ArrayList<>();
        treeList.add(tree0);
        treeList.add(tree1);
        treeList.add(tree2);
        treeList.add(tree3);
        treeList.add(tree4);
        treeList.add(tree5);
        treeList.add(tree6);
        treeList.add(tree7);
        treeList.add(tree8);
        treeList.add(tree9);
        treeList.add(tree10);
        treeList.add(tree11);
        treeList.add(tree12);
        treeList.add(tree13);

        List<Tree> trees = Tree.buildTree(treeList);
        printTree(trees);
    }

    private static void printTree(List<Tree> trees){
        for(Tree tree : trees){
            String prefix = "----";
            for(int i=0;i<tree.getLevel();i++){
                prefix += "----";
            }
            System.out.println(prefix+tree);
            if(tree.getNode() != null && tree.getNode().size() > 0){
                printTree(tree.getNode());
            }
        }
    }
}

###运行结果

----Tree{code=‘null’, name=‘根节点’, parentCode=‘null’, level=0}
--------Tree{code=‘1’, name=‘根节点’, parentCode=‘null’, level=1}
--------Tree{code=‘2’, name=‘根节点’, parentCode=‘null’, level=1}
------------Tree{code=‘3’, name=‘根节点’, parentCode=‘2’, level=2}
------------Tree{code=‘4’, name=‘根节点’, parentCode=‘2’, level=2}
----Tree{code=‘5’, name=‘根节点’, parentCode=‘0’, level=0}
--------Tree{code=‘9’, name=‘根节点’, parentCode=‘5’, level=1}
--------Tree{code=‘10’, name=‘根节点’, parentCode=‘5’, level=1}
------------Tree{code=‘13’, name=‘根节点’, parentCode=‘10’, level=2}
----------------Tree{code=‘11’, name=‘根节点’, parentCode=‘13’, level=3}
----------------Tree{code=‘12’, name=‘根节点’, parentCode=‘13’, level=3}
----Tree{code=‘6’, name=‘根节点’, parentCode=‘0’, level=0}
--------Tree{code=‘7’, name=‘根节点’, parentCode=‘6’, level=1}
------------Tree{code=‘8’, name=‘根节点’, parentCode=‘7’, level=2}

个人github代码地址
https://github.com/jokerChen1991/pattern/blob/master/src/com/jokerchen/pattern/structure/composite/

个人原创,转载请注明 https://blog.csdn.net/Joker_2007/article/details/82894857

展开阅读全文

没有更多推荐了,返回首页