跟着汤阳光同志做一个OA系统(七):树形处理(递归)

79 篇文章 1 订阅
35 篇文章 0 订阅

显示树形部门练习

package com.xsy.oa.test;

 

import java.util.ArrayList;

import java.util.Collection;

import java.util.LinkedHashSet;

import java.util.List;

import java.util.Set;

 

import org.junit.Test;

 

import com.xsy.oa.domain.Department;;

 

/**

 * 说明:不能使用多层循环的方式,因为需要能支持任意层。

 */

publicclass TreeViewPractice {

 

    /**

     * 练习一:打印所有顶层部门及其子孙部门的信息(名称)提示:假设有一个打印部门树的信息的方法

     *

     * 要求打印如下效果:

     *

     * <pre>

     * 市场部

     * 宣传部

     * 业务部

     * 业务一部

     * 业务二部

     * 开发部

     * 开发一部

     * 开发二部

     * </pre>

     */

    @Test

    publicvoid printAllDepts_1() {

       List<Department> topList = findTopLevelDepartmentList();

 

       // // 方式一

       // for (Department top :topList) {

       // showTree(top);

       // }

 

       // 方式二

       showTreeList(topList);

    }

 

    /**

     * 显示一颗部门树中所有节点的信息

     *

     * @param top

     *            树的顶点(根节点)

     */

    privatevoid showTree(Department top) {

       // 顶点

       System.out.println(top.getName());

       // 子树

       for (Department child : top.getChildren()) {

           showTree(child);

       }

    }

 

    /**

     * 显示多颗树的所有节点的信息

     *

     * @param topList

     */

    privatevoid showTreeList(Collection<Department> topList) {

       for (Department top : topList) {

           // 顶点

           System.out.println(top.getName());

           // 子树

           showTreeList(top.getChildren());

       }

    }

 

    /**

     * 练习二:打印所有顶层部门及其子孙部门的信息(名称),用不同的缩进表示层次(使用全角空格)。<br>

     * 子部门的名称前比上级部门多一个空格,最顶层部门的名字前没有空格。提示:假设有一个打印部门集合中所有部门信息的方法

     *

     * 要求打印如下效果:

     *

     * <pre>

     * 市场部

     *    宣传部

     *    业务部

     *       业务一部

     *       业务二部

     * 开发部

     *    开发一部

     *    开发二部

     * </pre>

     */

    @Test

    publicvoid printAllDepts_2() {

       List<Department> topList = findTopLevelDepartmentList();

 

       showTreeList_2(topList, "");

    }

 

    // 显示树

    privatevoid showTreeList_2(Collection<Department> topList,String prefix) {

       for (Department top : topList) {

           // 顶点

           System.out.println(prefix + top.getName());

           // 子树

           showTreeList_2(top.getChildren(), "    " + prefix);

       }

    }

 

    /**

     * 结构如下:

     *

     * <pre>

     * 市场部

     *    宣传部

     *    业务部

     *       业务一部

     *       业务二部

     * 开发部

     *    开发一部

     *    开发二部

     * </pre>

     *

     * @return所有最顶层的部门的列表

     */

    publicstatic List<Department> findTopLevelDepartmentList() {

       Department dept_1_1 = new Department();

       dept_1_1.setId(new Long(11));

       dept_1_1.setName("宣传部");

 

       Department dept_1_2 = new Department();

       dept_1_2.setId(new Long(12));

       dept_1_2.setName("业务部");

 

       Department dept_1_2_1 = new Department();

       dept_1_2_1.setId(new Long(121));

       dept_1_2_1.setName("业务一部");

 

       Department dept_1_2_2 = new Department();

       dept_1_2_2.setId(new Long(122));

       dept_1_2_2.setName("业务二部");

 

       dept_1_2_1.setParent(dept_1_2);

       dept_1_2_2.setParent(dept_1_2);

       Set<Department> children_0 = new LinkedHashSet<Department>();

       children_0.add(dept_1_2_1);

       children_0.add(dept_1_2_2);

       dept_1_2.setChildren(children_0);

 

       // ================================

 

       Department dept_1 = new Department();

       dept_1.setId(new Long(1));

       dept_1.setName("市场部");

 

       dept_1_1.setParent(dept_1);

       dept_1_2.setParent(dept_1);

       Set<Department> children_1 = new LinkedHashSet<Department>();

       children_1.add(dept_1_1);

       children_1.add(dept_1_2);

       dept_1.setChildren(children_1);

 

       // ---

 

       Department dept_2_1 = new Department();

       dept_2_1.setId(new Long(21));

       dept_2_1.setName("开发一部");

 

       Department dept_2_2 = new Department();

       dept_2_2.setId((new Long(22)));

       dept_2_2.setName("开发二部");

 

       Department dept_2 = new Department();

       dept_2.setId(new Long(2));

       dept_2.setName("开发部");

 

       dept_2_1.setParent(dept_2);

       dept_2_2.setParent(dept_2);

       Set<Department> children_2 = new LinkedHashSet<Department>();

       children_2.add(dept_2_1);

       children_2.add(dept_2_2);

       dept_2.setChildren(children_2);

 

       // ---

 

       List<Department> depts = new ArrayList<Department>();

       depts.add(dept_1);

       depts.add(dept_2);

       return depts;

    }

 

}

 

 

Action中准备好经过修改名字的部门

publicclass DepartmentUtils {

 

    /**

     * 遍历部门树,把所有的部门遍历出来放到同一个集合中返回,并且其中所有部门的名称都修改了,以表示层次。

     *

     * @param topList,几个顶级部门

     * @return

     */

    publicstatic List<Department>getAllDepartments(List<Department> topList) {

       List<Department> list = new ArrayList<Department>();

       walkDepartmentTreeList(topList, "", list);

       return list;

    }

 

    /**

     * 遍历部门树,把遍历出的部门信息放到指定的集合中

     *

     * @param topList

     */

    privatestaticvoid walkDepartmentTreeList(Collection<Department>topList, String prefix, List<Department> list) {

       for (Department top : topList) {

           // 顶点

           Department copy = new Department(); // 使用副本,因为原对象在Session

           copy.setId(top.getId());

           copy.setName(prefix + top.getName());

           list.add(copy); // 把副本添加到同一个集合中

 

           // 子树

           walkDepartmentTreeList(top.getChildren(), "" + prefix, list); // 使用全角的空格

       }

    }


 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值