未知昵称
2019-06-25 14:57
采纳率: 89.7%
浏览 450
已采纳

java定义几个类和ArrayList,存储读如下结构的数据??

有类似如下的结构,有A1,A2,A2...。A1下一级有B1,B2,B3..。B1下面有C1,C2等。。

怎么定义几个类和ArrayList,把这种结构的数据存储到ArrayList中。

再去把ArrayList中按照如上结构存储的数据读出来.

图片说明

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • catfishlyf 2019-06-25 15:48
    已采纳

    这种多层次的结构建议你用树形结构进行存储。定义的实体类只需要两个,并且所有数据也可以保存在数据库的一张表里,下面给你举个简单的例子:
    假如说你的实体为Menu,定义一个实体类:

    public class Menu {
    
        /**
         * 主键ID。
         */
        private String id;
    
        /**
         * 名称。
         */
        private String name;
    
        /**
         * 父ID。
         */
        private String parentId;// 父ID
    
        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 getParentId() {
            return parentId;
        }
    
        public void setParentId(String parentId) {
            this.parentId = parentId;
        }
    }
    
    

    再定义一个树形实体,代码如下:

    public class Tree<T> {
    
        /**
         * 节点ID。
         */
        private String id;
    
        /**
         * 显示节点名称。
         */
        private String name;
    
        /**
         * 节点的子节点。
         */
        private List<Tree<T>> child = new ArrayList<>();
    
        /**
         * 父ID。
         */
        private String parentId;
    
        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 List<Tree<T>> getChild() {
            return child;
        }
    
        public void setChild(List<Tree<T>> child) {
            this.child = child;
        }
    
        public String getParentId() {
            return parentId;
        }
    
        public void setParentId(String parentId) {
            this.parentId = parentId;
        }
    
        /**
         * 声明。
         * 
         * @param id 参数
         * @param name 参数
         * @param child 参数
         * @param parentId 参数
         */
    
        public Tree(String id, String name,  List<Tree<T>> child, String parentId) {
            super();
            this.id = id;
            this.name = name;
            this.child = child;
            this.parentId = parentId;
        }
    
        public Tree() {
            super();
        }
    
        @Override
        public String toString() {
    
            return JSON.toJSONString(this);
        }
    
    }
    

    数据中的数据存储如下:

    图片说明

    下面将数据库中的数据查出,并转化为树形菜单,首先将menu表中所有的数据查出来放到List

    menu中(这步不做过多叙述),然后
    通过getMenuTree()方法转为树形结构
        private static List<Menu> getMenuTree(List<Menu> menu) {
            List<Tree<Menu>> trees = new ArrayList<>();
            for (Menu m : menu) {
                Tree<Menu> tree = new Tree<>();
                tree.setId(m.getId());
                tree.setParentId(m.getParentId());
                tree.setName(m.getName());
                trees.add(tree);
            }
            List<Tree<Menu>> t = BuildTree.buildList(trees, "");
            return t;
        }
    
    点赞 打赏 评论
  • dabocaiqq 2019-06-25 15:30

    java list实现树形结构

    1.javabean

    import java.util.List;

    public class TreeNode {

    private String id;

    private String parentId;

    private String name;

    private List children;

    public TreeNode(String id, String name, String parentId) {
    this.id = id;
    this.parentId = parentId;
    this.name = name;
    }
    public TreeNode(String id, String name, TreeNode parent) {
    this.id = id;
    this.parentId = parent.getId();
    this.name = name;
    }

    public String getParentId() {
    return parentId;
    }

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

    public String getName() {
    return name;
    }

    public void setName(String name) {
    this.name = name;
    }

    public String getId() {
    return id;
    }

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

    public List getChildren() {
    return children;
    }

    public void setChildren(List children) {
    this.children = children;
    }

    @Override
    public String toString() {
    return "TreeNode{" +
    "id='" + id + '\'' +
    ", parentId='" + parentId + '\'' +
    ", name='" + name + '\'' +
    ", children=" + children +
    '}';
    }

    }

    2.方法

    import java.util.ArrayList;
    import java.util.List;
    public class TreeBuilder {

    /**

    • 两层循环实现建树
    • @param treeNodes 传入的树节点列表
    • @return */ public static List bulid(List treeNodes) {

    List trees = new ArrayList();

    for (TreeNode treeNode : treeNodes) {

    if ("0".equals(treeNode.getParentId())) {
    trees.add(treeNode);
    }

    for (TreeNode it : treeNodes) {
    if (it.getParentId() == treeNode.getId()) {
    if (treeNode.getChildren() == null) {
    treeNode.setChildren(new ArrayList());
    }
    treeNode.getChildren().add(it);
    }
    }
    }
    return trees;
    }

    /**

    • 使用递归方法建树
    • @param treeNodes
    • @return */ public static List buildByRecursive(List treeNodes) { List trees = new ArrayList(); for (TreeNode treeNode : treeNodes) { if ("0".equals(treeNode.getParentId())) { trees.add(findChildren(treeNode,treeNodes)); } } return trees; }

    /**

    • 递归查找子节点
    • @param treeNodes
    • @return */ public static TreeNode findChildren(TreeNode treeNode,List treeNodes) { for (TreeNode it : treeNodes) { if(treeNode.getId().equals(it.getParentId())) { if (treeNode.getChildren() == null) { treeNode.setChildren(new ArrayList()); } treeNode.getChildren().add(findChildren(it,treeNodes)); } } return treeNode; }

    public static void main(String[] args) {

    TreeNode treeNode1 = new TreeNode("1","广州","0");
    TreeNode treeNode2 = new TreeNode("2","深圳","0");

    TreeNode treeNode3 = new TreeNode("3","天河区",treeNode1);
    TreeNode treeNode4 = new TreeNode("4","越秀区",treeNode1);
    TreeNode treeNode5 = new TreeNode("5","黄埔区",treeNode1);
    TreeNode treeNode6 = new TreeNode("6","石牌",treeNode3);
    TreeNode treeNode7 = new TreeNode("7","百脑汇",treeNode6);

    TreeNode treeNode8 = new TreeNode("8","南山区",treeNode2);
    TreeNode treeNode9 = new TreeNode("9","宝安区",treeNode2);
    TreeNode treeNode10 = new TreeNode("10","科技园",treeNode8);

    List list = new ArrayList();

    list.add(treeNode1);
    list.add(treeNode2);
    list.add(treeNode3);
    list.add(treeNode4);
    list.add(treeNode5);
    list.add(treeNode6);
    list.add(treeNode7);
    list.add(treeNode8);
    list.add(treeNode9);
    list.add(treeNode10);
    3.测试
    List trees = TreeBuilder.bulid(list);
    for (int i = 0; i < trees.size(); i++) {
    System.out.println(trees.get(i));
    }
    List trees_ = TreeBuilder.buildByRecursive(list);
    for (int i = 0; i <trees.size(); i++) {
    System.out.println(trees.get(i));
    }
    }

    }

    4.数据

    TreeNode{id='1', parentId='0', name='广州', children=[TreeNode{id='3', parentId='1', name='天河区', children=[TreeNode{id='6', parentId='3', name='石牌', children=[TreeNode{id='7', parentId='6', name='百脑汇', children=null}]}]}, TreeNode{id='4', parentId='1', name='越秀区', children=null}, TreeNode{id='5', parentId='1', name='黄埔区', children=null}]}
    TreeNode{id='2', parentId='0', name='深圳', children=[TreeNode{id='8', parentId='2', name='南山区', children=[TreeNode{id='10', parentId='8', name='科技园', children=null}]}, TreeNode{id='9', parentId='2', name='宝安区', children=null}]}
    TreeNode{id='1', parentId='0', name='广州', children=[TreeNode{id='3', parentId='1', name='天河区', children=[TreeNode{id='6', parentId='3', name='石牌', children=[TreeNode{id='7', parentId='6', name='百脑汇', children=null}, TreeNode{id='7', parentId='6', name='百脑汇', children=null}]}, TreeNode{id='6', parentId='3', name='石牌', children=[TreeNode{id='7', parentId='6', name='百脑汇', children=null}, TreeNode{id='7', parentId='6', name='百脑汇', children=null}]}]}, TreeNode{id='4', parentId='1', name='越秀区', children=null}, TreeNode{id='5', parentId='1', name='黄埔区', children=null}, TreeNode{id='3', parentId='1', name='天河区', children=[TreeNode{id='6', parentId='3', name='石牌', children=[TreeNode{id='7', parentId='6', name='百脑汇', children=null}, TreeNode{id='7', parentId='6', name='百脑汇', children=null}]}, TreeNode{id='6', parentId='3', name='石牌', children=[TreeNode{id='7', parentId='6', name='百脑汇', children=null}, TreeNode{id='7', parentId='6', name='百脑汇', children=null}]}]}, TreeNode{id='4', parentId='1', name='越秀区', children=null}, TreeNode{id='5', parentId='1', name='黄埔区', children=null}]}
    TreeNode{id='2', parentId='0', name='深圳', children=[TreeNode{id='8', parentId='2', name='南山区', children=[TreeNode{id='10', parentId='8', name='科技园', children=null}, TreeNode{id='10', parentId='8', name='科技园', children=null}]}, TreeNode{id='9', parentId='2', name='宝安区', children=null}, TreeNode{id='8', parentId='2', name='南山区', children=[TreeNode{id='10', parentId='8', name='科技园', children=null}, TreeNode{id='10', parentId='8', name='科技园', children=null}]}, TreeNode{id='9', parentId='2', name='宝安区', children=null}]}

    点赞 打赏 评论

相关推荐 更多相似问题