DEMO_01:List数据存储,回调函数,集合转字符串,元素去重 ...

笑看天下无敌手  金牌会员 | 2024-6-20 11:07:07 | 来自手机 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 974|帖子 974|积分 2922

  1. * 题目:
  2. * 1. 构建属性结构List<DemoNode> data,根据本包的data.png中数据结构图将数据存入data中(字就是nodeName)
  3. * 2. 将树形结构List<DemoNode>里面的元素全部遍历出来存放到List<String> list中,输出结果转换成字符串:粉粉碎机被粉碎机粉碎了怎么办
  4. * 3. 将list里元素去重后存放到List<String> result,输出结果转换成字符串:粉碎机被了怎么办
复制代码

 
主类:
  1. /** * 稽核点:List数据存储,回调函数,集合转字符串,元素去重 *
  2. * 题目:
  3. * 1. 构建属性结构List<DemoNode> data,根据本包的data.png中数据结构图将数据存入data中(字就是nodeName)
  4. * 2. 将树形结构List<DemoNode>里面的元素全部遍历出来存放到List<String> list中,输出结果转换成字符串:粉粉碎机被粉碎机粉碎了怎么办
  5. * 3. 将list里元素去重后存放到List<String> result,输出结果转换成字符串:粉碎机被了怎么办 */public class Main01 {    public static void main(String[] args) {        // 树型数据存放        List data = new ArrayList();        // 构建元素        DemoNode demoNode = generateNodeTree();        data.add(demoNode);        // 遍历数据存放        List eleList = new ArrayList();        getAllEle(eleList, data);        System.out.println(String.join("", eleList));     // 粉粉碎机被粉碎机粉碎了怎么办        // 去重后元素存放        // method1: 使用set无法保证能按List次序排序        Set set = new HashSet(eleList);        List result1 = new ArrayList(set);        System.out.println(String.join("", result1)); // 了么粉被碎怎机办        // method2: 使用List.contains方法判断重存放        List result2 = new ArrayList();        for (String one : eleList) {            if (!result2.contains(one)) {                result2.add(one);            }        }        System.out.println(String.join("", result2)); // 粉碎机被了怎么办    }    /**     * 遍历树形结构(按层级从小到大)     *     * @param eleList  存放节点名称元素集合     * @param nodeList 要遍历的元素集合     */    static void getAllEle(List eleList, List nodeList) {        List childList = new ArrayList();        for (DemoNode one : nodeList) {            eleList.add(one.getNodeName());            if (one.getChild() != null && one.getChild().size() > 0) {                childList.addAll(one.getChild());            }        }        // 下一级有子节点,开始下一级遍历        if (childList != null && childList.size() > 0) {            getAllEle(eleList, childList);        }    }    /**     * 构建图片属性结构     *     * @return     */    private static DemoNode generateNodeTree() {        /** 构造第三层元素并归属 **/        DemoNode node31 = new DemoNode("被");        List node31Childs = addChildNodeList(new DemoNode("了"), new DemoNode("怎"));        node31.setChild(node31Childs);        DemoNode node33 = new DemoNode("碎");        List node33Childs = addChildNodeList(new DemoNode("么"), new DemoNode("办"));        node33.setChild(node33Childs);        /** 构造第二层元素并归属 **/        DemoNode node21 = new DemoNode("粉");        List node21Childs = addChildNodeList(node31, new DemoNode("粉"));        node21.setChild(node21Childs);        DemoNode node22 = new DemoNode("碎");        List node22Childs = addChildNodeList(node33, new DemoNode("机"));        node22.setChild(node22Childs);        DemoNode node23 = new DemoNode("机");        List node23Childs = addChildNodeList(new DemoNode("粉"), new DemoNode("碎"));        node23.setChild(node23Childs);        /** 构造第一层元素并归属 **/        DemoNode nodeRoot = new DemoNode("粉");        List nodeRootChilds = addChildNodeList(node21, node22, node23);        nodeRoot.setChild(nodeRootChilds);        return nodeRoot;    }    /**     * 组装子节点     *     * @param childNodes     * @return     */    private static List addChildNodeList(DemoNode... childNodes) {        List list = new ArrayList();        for (DemoNode one : childNodes) {            list.add(one);        }        return list;    }}
复制代码
 
节点类:
  1. /**
  2. * 节点数据
  3. */
  4. public class DemoNode {
  5.     private String NodeName; //节点名称
  6.     private List<DemoNode> child; //子节点
  7.     public String getNodeName() {
  8.         return NodeName;
  9.     }
  10.     public void setNodeName(String nodeName) {
  11.         NodeName = nodeName;
  12.     }
  13.     public List<DemoNode> getChild() {
  14.         return child;
  15.     }
  16.     public void setChild(List<DemoNode> child) {
  17.         this.child = child;
  18.     }
  19.     public DemoNode(String nodeName) {
  20.         NodeName = nodeName;
  21.     }
  22.     public DemoNode() {
  23.     }
  24. }
复制代码
 

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

笑看天下无敌手

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表