亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

java編程兩種樹形菜單結構的轉換代碼

發布時間:2020-08-23 02:57:47 來源:腳本之家 閱讀:248 作者:zhangzeyuaaa 欄目:編程語言

首先看看兩種樹形菜單結構的代碼示例。

SingleTreeNode:

package com.zzj.tree;
public class SingleTreeNode {
	private int id;
	private int pId;
	private String name;
	public SingleTreeNode() {
	}
	public SingleTreeNode(int id, int pId, String name) {
		this.id = id;
		this.pId = pId;
		this.name = name;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getpId() {
		return pId;
	}
	public void setpId(int pId) {
		this.pId = pId;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Override 
	  public String toString() {
		return "SingleTreeNode [id=" + id + ", pId=" + pId + ", name=" + name + "]";
	}
}

這種結構很適合存儲到關系型數據庫中。

MultiTreeNode:

package com.zzj.tree;
import java.util.List;
public class MultiTreeNode {
	private int id;
	private String name;
	private List<MultiTreeNode> children;
	public MultiTreeNode() {
	}
	public MultiTreeNode(int id, String name) {
		this.id = id;
		this.name = name;
	}
	public MultiTreeNode(int id, String name, List<MultiTreeNode> children) {
		this.id = id;
		this.name = name;
		this.children = children;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public List<MultiTreeNode> getChildren() {
		return children;
	}
	public void setChildren(List<MultiTreeNode> children) {
		this.children = children;
	}
	@Override 
	  public String toString() {
		return "MultiTreeNode [id=" + id + ", name=" + name + ", children=" + children + "]";
	}
}

兩種轉換器

ToMultiTreeTransformer:

package com.zzj.tree;
import java.util.ArrayList;
import java.util.List;
public class ToMultiTreeTransformer {
	private List<SingleTreeNode> singleTreeNodes;
	private List<MultiTreeNode> multiTreeNodes = new ArrayList<>();
	public ToMultiTreeTransformer(List<SingleTreeNode> singleTreeNodes) {
		this.singleTreeNodes = singleTreeNodes;
	}
	public List<MultiTreeNode> transform(){
		// 先找出所有的根節點 
		for (int i = 0; i < singleTreeNodes.size(); i++) {
			SingleTreeNode singleTreeNode = singleTreeNodes.get(i);
			Boolean isRoot = true;
			for (int j = 0; j < singleTreeNodes.size(); j++) {
				SingleTreeNode temp = singleTreeNodes.get(j);
				if (singleTreeNode.getpId() == temp.getId()) {
					isRoot = false;
					break;
				}
			}
			if (isRoot) {
				MultiTreeNode multiTreeNode = new MultiTreeNode(singleTreeNode.getId(),  
				            singleTreeNode.getName());
				multiTreeNodes.add(multiTreeNode);
			}
		}
		// 設置子節點 
		for (int i = 0; i < multiTreeNodes.size(); i++) {
			MultiTreeNode multiTreeNode = multiTreeNodes.get(i);
			setChildren(multiTreeNode);
		}
		return multiTreeNodes;
	}
	/** 
   * 設置子節點 
   * @param multiTreeNode 
   */
	private void setChildren(MultiTreeNode multiTreeNode){
		for (int j = 0; j < singleTreeNodes.size(); j++) {
			SingleTreeNode temp = singleTreeNodes.get(j);
			if (temp.getpId() == multiTreeNode.getId()) {
				MultiTreeNode child = new MultiTreeNode(temp.getId(),  
				            temp.getName());
				List<MultiTreeNode> children = multiTreeNode.getChildren();
				if (children == null) {
					children = new ArrayList<>();
					multiTreeNode.setChildren(children);
				}
				children.add(child);
				setChildren(child);
			}
		}
	}
}

ToSingleTreeTransformer:

package com.zzj.tree;
import java.util.ArrayList;
import java.util.List;
public class ToSingleTreeTransformer {
	private List<MultiTreeNode> multiTreeNodes;
	private List<SingleTreeNode> singleTreeNodes = new ArrayList<>();
	public ToSingleTreeTransformer(List<MultiTreeNode> multiTreeNodes) {
		this.multiTreeNodes = multiTreeNodes;
	}
	public List<SingleTreeNode> transform(){
		// 先獲取根節點 
		for (int i = 0; i < multiTreeNodes.size(); i++) {
			MultiTreeNode multiTreeNode = multiTreeNodes.get(i);
			SingleTreeNode singleTreeNode = new SingleTreeNode(multiTreeNode.getId(),  
			          0, multiTreeNode.getName());
			singleTreeNodes.add(singleTreeNode);
		}
		// 獲取子節點 
		for (int i = 0; i < multiTreeNodes.size(); i++) {
			MultiTreeNode multiTreeNode = multiTreeNodes.get(i);
			getChildren(multiTreeNode);
		}
		return singleTreeNodes;
	}
	/** 
   * 獲取子節點 
   * @param multiTreeNode 
   */
	private void getChildren(MultiTreeNode multiTreeNode){
		for (int i = 0; i < singleTreeNodes.size(); i++) {
			SingleTreeNode temp = singleTreeNodes.get(i);
			if (multiTreeNode.getId() == temp.getId()) {
				List<MultiTreeNode> children = multiTreeNode.getChildren();
				if (children != null) {
					for (int j = 0; j < children.size(); j++) {
						MultiTreeNode child = children.get(j);
						SingleTreeNode singleTreeNode = new SingleTreeNode(child.getId(),  
						                multiTreeNode.getId(), child.getName());
						singleTreeNodes.add(singleTreeNode);
						getChildren(child);
					}
				}
			}
		}
	}
}

測試

package com.zzj.tree;
import java.util.ArrayList;
import java.util.List;
public class TreeTransformerTest {
	private final static List<SingleTreeNode> SINGLE_TREE = new ArrayList<SingleTreeNode>();
	static {
		SingleTreeNode China = new SingleTreeNode(1, 0, "中國");
		SINGLE_TREE.add(China);
		SingleTreeNode Hunan = new SingleTreeNode(2, 1, "湖南");
		SINGLE_TREE.add(Hunan);
		SingleTreeNode Changsha = new SingleTreeNode(3, 2, "長沙");
		SINGLE_TREE.add(Changsha);
		SingleTreeNode Hubei = new SingleTreeNode(4, 1, "湖北");
		SINGLE_TREE.add(Hubei);
		SingleTreeNode Wuhan = new SingleTreeNode(5, 4, "武漢");
		SINGLE_TREE.add(Wuhan);
		SingleTreeNode America = new SingleTreeNode(6, 0, "美國");
		SINGLE_TREE.add(America);
		SingleTreeNode California = new SingleTreeNode(7, 6, "加利福尼亞");
		SINGLE_TREE.add(California);
		SingleTreeNode LosAngeles = new SingleTreeNode(8, 7, "洛杉磯");
		SINGLE_TREE.add(LosAngeles);
	}
	public static void main(String[] args) throws Exception {
		ToMultiTreeTransformer multiTreeTransformer = new ToMultiTreeTransformer(SINGLE_TREE);
		List<MultiTreeNode> multiTreeNodes = multiTreeTransformer.transform();
		System.out.println(multiTreeNodes);
		ToSingleTreeTransformer singleTreeTransformer = new ToSingleTreeTransformer(multiTreeNodes);
		List<SingleTreeNode> singleTreeNodes = singleTreeTransformer.transform();
		System.out.println(singleTreeNodes);
	}
}

輸出結果:

[MultiTreeNode [id=1, name=中國, children=[MultiTreeNode [id=2, name=湖南, children=[MultiTreeNode [id=3, name=長沙, children=null]]], MultiTreeNode [id=4, name=湖北, children=[MultiTreeNode [id=5, name=武漢, children=null]]]]], MultiTreeNode [id=6, name=美國, children=[MultiTreeNode [id=7, name=加利福尼亞, children=[MultiTreeNode [id=8, name=洛杉磯, children=null]]]]]] 
[SingleTreeNode [id=1, pId=0, name=中國], SingleTreeNode [id=6, pId=0, name=美國], SingleTreeNode [id=2, pId=1, name=湖南], SingleTreeNode [id=3, pId=2, name=長沙], SingleTreeNode [id=4, pId=1, name=湖北], SingleTreeNode [id=5, pId=4, name=武漢], SingleTreeNode [id=7, pId=6, name=加利福尼亞], SingleTreeNode [id=8, pId=7, name=洛杉磯]] 

總結

以上就是本文關于java編程兩種樹形菜單結構的轉換代碼的全部內容,希望對大家有所幫助。感興趣的朋友可以繼續參閱本站:

Java實現生成Excel樹形表頭完整代碼示例

Java語言描述二叉樹的深度和寬度

java算法實現紅黑樹完整代碼示例

如有不足之處,歡迎留言指出!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

田东县| 芷江| 海伦市| 大埔区| 西贡区| 陵川县| 东乡族自治县| 彭水| 泸溪县| 万载县| 宜兰市| 贺州市| 财经| 莱阳市| 卓尼县| 武义县| 佛坪县| 梨树县| 栖霞市| 潞西市| 青冈县| 界首市| 河南省| 昭平县| 化州市| 华蓥市| 阳东县| 登封市| 台安县| 龙井市| 佳木斯市| 建平县| 赤水市| 堆龙德庆县| 张家川| 白沙| 玉林市| 营口市| 望谟县| 同江市| 澄江县|