程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> java 根據 根節點及所有子成員 構造樹tree,javatree

java 根據 根節點及所有子成員 構造樹tree,javatree

編輯:JAVA綜合教程

java 根據 根節點及所有子成員 構造樹tree,javatree


 

//entity

package com.ompa.biz.entity;

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

public class TreeEntity {

private String id;
private String name;
private String fatherId;
private int level;
private boolean isleaf;
private List<TreeEntity> childList = new ArrayList<TreeEntity>();

public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getFatherId() {
return fatherId;
}
public void setFatherId(String fatherId) {
this.fatherId = fatherId;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
public boolean isIsleaf() {
return isleaf;
}
public void setIsleaf(boolean isleaf) {
this.isleaf = isleaf;
}
public List<TreeEntity> getChildList() {
return childList;
}
public void setChildList(List<TreeEntity> childList) {
this.childList = childList;
}

}

package com.ompa.utils;

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

import org.apache.commons.collections.CollectionUtils;

import com.ompa.biz.entity.TreeEntity;

/**
* 樹結構工具類
* <p>Title:TreeUtil </p>
* @author zhangcd
* @date 2016年11月10日
*/
public class TreeUtil {

/**
* 根據父節點,將子節點一次累計起來
* @author zhangcd
* @date 2016年11月10日上午9:40:33
* @param root
* @param childList
* @return
*/
public TreeEntity getTreeList(TreeEntity root,List<TreeEntity> childList){
root.setLevel(1);
findChildren(root,childList);
return root;
}

private List<TreeEntity> findChildren(TreeEntity root, List<TreeEntity> allNodes) {
List<TreeEntity> children = new ArrayList<TreeEntity>();

for (TreeEntity comparedOne : allNodes) {
if (comparedOne.getFatherId().equals(root.getId())) {
root.getChildList().add(comparedOne);
comparedOne.setLevel(root.getLevel() + 1);
children.add(comparedOne);
}
}
List<TreeEntity> notChildren = (List<TreeEntity>) CollectionUtils.subtract(allNodes, children);

for (TreeEntity child : children) {
List<TreeEntity> tmpChildren = findChildren(child, notChildren);
if (tmpChildren == null || tmpChildren.size() < 1) {
child.setIsleaf(true);
} else {
child.setIsleaf(false);
}
// child.setChildren(tmpChildren);
}
return children;
}

/**
* 得到深度
*
* @author zhangcd
* @date 2016-6-13
* @param rowspan
* @param step
* @return
*/
private int getrowspan(int rowspan,TreeEntity step){
if(step.getChildList() != null && step.getChildList().size()>0){
for(TreeEntity steps:step.getChildList()){
if(rowspan < steps.getLevel()){
rowspan = steps.getLevel();
}
rowspan = getrowspan(rowspan,steps);
}
}
return rowspan;
}

/**
* 合並列數
*
* @author zhangcd
* @date 2016-6-12
* @param colspan
* @param step
* @return
*/
private int getcolspan(int colspan,TreeEntity step){
if(step.getChildList() != null && step.getChildList().size()>0){
for(TreeEntity steps:step.getChildList()){
colspan = getcolspan(colspan,steps);
}
}else{
colspan ++;
}
return colspan;
}

}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved