如何在Java中返回樹形結構 最佳實踐

self_control發表於2014-12-17

點選(此處)摺疊或開啟

  1. import java.util.ArrayList;
  2. import java.util.HashMap;
  3. import java.util.List;
  4. import java.util.Map;

  5. public class TestTree {
  6.     public static void main(String[] args) {
  7.         TestData data = new TestData();
  8.         Map<String, TreeNode> midStr = new HashMap<String, TreeNode>();
  9.         
  10.         /**把list中的資料 放到 map中,方便後續的查詢*/
  11.         for (int n = 0; n < data.listTreeNode.size(); n++) {
  12.             midStr.put(data.listTreeNode.get(n).getId(),
  13.                     data.listTreeNode.get(n));
  14.         }
  15.         
  16.         String root = null; //用來儲存根節點的id
  17.         
  18.         /*關鍵操作
  19.          * 遍歷 map 中的TreeNode 找到 父親節點,如果找不到父親節點,那這個TreeNode就是根節點。
  20.          * 把自己放到父親節點的 children 列表中
  21.          */
  22.         for (Map.Entry<String, TreeNode> entry : midStr.entrySet()) {
  23.             TreeNode t = midStr.get(entry.getValue().getParentId());
  24.             if (t != null)
  25.                 midStr.get(entry.getValue().getParentId()).getChildren()
  26.                         .add(entry.getValue());
  27.             else
  28.                 root = entry.getKey();
  29.         }
  30.         System.out.println(midStr.get(root));
  31.         TreeNode ret = midStr.get(root);
  32.         System.out.println(\"\");

  33.     }
  34. }

  35. /**
  36.  * 樹形結構
  37.  * */
  38. class TreeNode {
  39.     private String id;
  40.     private String parentId;
  41.     private List<TreeNode> children = new ArrayList<TreeNode>();;

  42.     public TreeNode(String p_id, String p_parentId) {
  43.         this.id = p_id;
  44.         this.parentId = p_parentId;
  45.     }

  46.     public String getId() {
  47.         return id;
  48.     }

  49.     public void setId(String id) {
  50.         this.id = id;
  51.     }

  52.     public String getParentId() {
  53.         return parentId;
  54.     }

  55.     public void setParentId(String parentId) {
  56.         this.parentId = parentId;
  57.     }

  58.     public List<TreeNode> getChildren() {
  59.         return children;
  60.     }

  61.     public void setChildren(List<TreeNode> children) {
  62.         this.children = children;
  63.     }

  64. }

  65. /**
  66.  * 模擬資料庫返回資料 List <DTO>
  67.  * */
  68. class TestData {
  69.     public List<TreeNode> listTreeNode = new ArrayList<TreeNode>();
  70.     TestData() {
  71.         listTreeNode.add(new TreeNode("1", "0"));

  72.         listTreeNode.add(new TreeNode("7", "3"));
  73.     
  74.         listTreeNode.add(new TreeNode("2", "1"));

  75.         listTreeNode.add(new TreeNode("3", "1"));

  76.         listTreeNode.add(new TreeNode("4", "2"));

  77.         listTreeNode.add(new TreeNode("5", "2"));

  78.         listTreeNode.add(new TreeNode("6", "1"));
  79.     }
  80. }


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/30066956/viewspace-1371172/,如需轉載,請註明出處,否則將追究法律責任。

相關文章