自定义tree工具类 TreeUtils.java

自定义tree工具类 TreeUtils.java

简介

  • 自定义tree工具类

api

  • 将参数list 构造成 tree *getTree(List list);*

源码

  • import java.util.Collections;
    import java.util.List;
    import java.util.Map;
    import java.util.stream.Collectors;
     
    /**
     * @说明:tree工具类
     */
    public class TreeUtils {
    	
    	/**
    	 * 方法功能说明:@1.将参数list 构造成 tree
    	 */
        public static List getTree(List list) {
        	Collections.sort(list);
            Map> son = list.stream().filter(node -> !"0".equals(node.getParentId()))
            		.collect(Collectors.groupingBy(node -> node.getParentId()));
            list.forEach(node -> node.setChildList(son.get(node.getNodeId())));
            return list.stream().filter(node -> "0".equals(node.getParentId())).collect(Collectors.toList());
        }
    	
    	/**
    	 * @说明:tree节点
    	 */
    	public class Node implements Comparable {
    		
    		private String nodeId;
    		private String nodeName;
    		private String parentId;
    		private Long sort;
    		
    		public Long getSort() {
    			return sort;
    		}
    		public void setSort(Long sort) {
    			this.sort = sort;
    		}
    		public String getNodeId() {
    			return nodeId;
    		}
    		public void setNodeId(String nodeId) {
    			this.nodeId = nodeId;
    		}
    		public String getNodeName() {
    			return nodeName;
    		}
    		public void setNodeName(String nodeName) {
    			this.nodeName = nodeName;
    		}
    		public String getParentId() {
    			return parentId;
    		}
    		public void setParentId(String parentId) {
    			this.parentId = parentId;
    		}
    		
    		public Node(String nodeId, String nodeName, String parentId, Long sort) {
    			this.nodeId = nodeId;
    			this.nodeName = nodeName;
    			this.parentId = parentId;
    			this.sort = sort;
    		}
    		
    		@Override
    		public boolean equals(Object obj) {
    			if (this == obj) { return true; }
    			if (obj == null) { return false; }
    			if (getClass() != obj.getClass()) { return false; }
    			Node other = (Node) obj;
    			if (nodeId == null) {
    				if (other.nodeId != null) { return false; }
    			} else if (!nodeId.equals(other.nodeId)) {
    				return false;
    			}
    			if (nodeName == null) {
    				if (other.nodeName != null) { return false; }
    			} else if (!nodeName.equals(other.nodeName)) {
    				return false;
    			}
    			if (parentId == null) {
    				if (other.parentId != null) { return false; }
    			} else if (!parentId.equals(other.parentId)) {
    				return false;
    			}
    			if (sort == null) {
    				if (other.sort != null) { return false; }
    			} else if (!sort.equals(other.sort)) {
    				return false;
    			}
    			return true;
    		}
     
    		/**
    		 * 下级节点集合
    		 */
    		private List childList;
     
    		public List getChildList() {
    			return childList;
    		}
    		public void setChildList(List childList) {
    			this.childList = childList;
    		}
    		@Override
    		public int compareTo(Node node) {
    			//正序
    			return this.sort.compareTo(node.sort);
    			//倒序
    //			return node.sort.compareTo(this.sort);
    		}
    	}
    	
    }
    

你可能感兴趣的:(JAVA基础工作中实际总结,编程学习,java,windows,开发语言)