ITPub博客

首页 > 应用开发 > Java > 如何在Java中返回树形结构 最佳实践

如何在Java中返回树形结构 最佳实践

原创 Java 作者:self_control 时间:2014-12-17 16:17:46 0 删除 编辑

点击(此处)折叠或打开

  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博客 ” ,链接:https://blog.itpub.net/30066956/viewspace-1371172/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2014-12-16

  • 博文量
    49
  • 访问量
    406893