美文网首页java
基于Lamada表达式的树结构遍历

基于Lamada表达式的树结构遍历

作者: singlezero | 来源:发表于2019-04-04 11:49 被阅读0次

最近在做项目中需要做部门树的生成, 之前我们有两种方法,一种是mysql中写存储过程用递归的方法生成,另一种是java中用for循环递归的方式,最近在网上看到大神有用Lamada表达式的方法,觉得这样写很简洁很直观,基于我们的需求所以写了一个demo,有不对的地方,望大神指正.

package com.yihur.demo;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.junit.Before;
import org.junit.Test;

import java.util.*;

/**
 * @author yihur
 * @description 树结构遍历---已知子节点遍历子节点的所有叶子节点
 * @date 2019/4/1
 */
public class TreeTestDemo {

    private List<Map<String,Object>> bodyList;

    private List<Map<String,Object>> rootList;

    //用于存放该节点下所有节点,
    private List<Map<String,Object>> beanList;

    @Before
    public void before_new_list(){
        bodyList = new ArrayList<>();
        rootList = new ArrayList<>();
        beanList = new ArrayList<>();
    }

    /**
     * 所有节点
     * @author yihur
     * @date 2019/4/4
     * @param
     * @return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     */
    private List<Map<String,Object>> selectAllTreeNode(){
        List<Map<String,Object>> resultReturn= new ArrayList<>();
        Map<String,Object> map = new HashMap<>();

        map.put("id","100");
        map.put("parentID","10");
        resultReturn.add(map);
        map = new HashMap<>();

        map.put("id","200");
        map.put("parentID","100");
        resultReturn.add(map);
        map = new HashMap<>();

        map.put("id","201");
        map.put("parentID","100");
        resultReturn.add(map);
        map = new HashMap<>();

        map.put("id","300");
        map.put("parentID","200");
        resultReturn.add(map);
        map = new HashMap<>();

        map.put("id","301");
        map.put("parentID","201");
        resultReturn.add(map);
        map = new HashMap<>();

        map.put("id","400");
        map.put("parentID","300");
        resultReturn.add(map);
        map = new HashMap<>();

        map.put("id","401");
        map.put("parentID","301");
        resultReturn.add(map);
        map = new HashMap<>();

        map.put("id","500");
        map.put("parentID","400");
        resultReturn.add(map);
        map = new HashMap<>();

        map.put("id","501");
        map.put("parentID","401");
        resultReturn.add(map);

        return resultReturn;
    }

    /**
     * 根节点 或 起始节点
     * @author yihur
     * @date 2019/4/4
     * @param
     * @return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     */
    private List<Map<String,Object>> selectRootTreeNode(){
        List<Map<String,Object>> resultReturn= new ArrayList<>();
        Map<String,Object> map = new HashMap<>();

        map.put("id","100");
        map.put("parentID","10");
        resultReturn.add(map);
        map = new HashMap<>();

        map.put("id","200");
        map.put("parentID","100");
        resultReturn.add(map);

        return resultReturn;
    }


    @Test
    public void test_tree_node_by_current_node(){
        List<Map<String,Object>> rootList = selectRootTreeNode();
        bodyList = selectAllTreeNode();
        // newHashMapWithExpectedSize是guava中的方法,用于初始化一个特定大小的HashMap
        Map<String, String> map = Maps.newHashMapWithExpectedSize(bodyList.size());
        rootList.forEach(beanTree -> getChild(beanTree, map));

        System.out.println(Arrays.toString(rootList.toArray()));
        beanList.addAll(rootList);
        System.out.println(Arrays.toString(beanList.toArray()));
        System.out.println(Arrays.toString(bodyList.toArray()));
    }

    /**
     * 方法描述
     * @author yihur
     * @date 2019/4/4
     * @param beanTree
     * @param map
     * @return void
     */
    private void getChild(Map<String, Object> beanTree ,Map<String, String> map) {
        List<Map<String,Object>> childList = Lists.newArrayList();
        bodyList.stream()
                .filter(c -> !map.containsKey(c.get("id").toString()))
                .filter(c -> c.get("parentID").toString().equals(beanTree.get("id").toString()))
                .forEach(c -> {
                    map.put(c.get("id").toString(), c.get("parentID").toString());
                    getChild(c, map);
                    childList.add(c);
                });
        // 所有叶子结点不加childList参数,避免叶子节点带有该参数下,前端控件依然显示加号
        if (childList.size() != 0) {
            beanTree.put("childList",childList);
            beanList.addAll(childList);
        }
    }


}


相关文章

  • 基于Lamada表达式的树结构遍历

    最近在做项目中需要做部门树的生成, 之前我们有两种方法,一种是mysql中写存储过程用递归的方法生成,另一种是ja...

  • java 的lamada表达式

    在使用C#时,可以使用Lamada表达式在集合中进行检索,比如下面的代码: java的集合没有Lamada的扩展,...

  • 654. Maximum Binary Tree

    思路:正常遍历生成树结构即可代码:

  • 二叉树的后序遍历(递归和非递归版本)

    难易程度:★★ 重要性:★★★★★ 树结构是面试中的考察的重点,而树的遍历又是树结构的基础。非递归的前序遍历算法思路可以

  • 二叉树的中序遍历(递归和非递归版本)

    难易程度:★★ 重要性:★★★★★ 树结构是面试中的考察的重点,而树的遍历又是树结构的基础。中序遍历的非递归版本要...

  • 树结构的遍历

    1.树的定义 一棵树(tree)是由n(n>0)个元素组成的[有限集合],其中: 每个元素称为结点(node); ...

  • 【转】js数组和树结构数据相互转换

    数组转树结构采取递归和非递归两种方式,树结构转扁平化数组采取深度优先遍历(递归和非递归两种方式)和广度优先遍历实现...

  • 递归遍历树结构

    调用:

  • JS树结构操作

    一、遍历树结构 1. 树结构介绍 JS中树结构一般是类似于这样的结构: 为了更通用,可以用存储了树根节点的列表表示...

  • 树结构中的每个节点有其父节点和子节点: 对于树的遍历,一般有三种,先序遍历,中序遍历和后序遍历: 先序遍历:这里以...

网友评论

    本文标题:基于Lamada表达式的树结构遍历

    本文链接:https://www.haomeiwen.com/subject/mpjfiqtx.html