您的位置:首页 > 编程语言 > Java开发

【Java核心技术】用于自连接的递归调用示例

2012-11-04 16:26 411 查看
package cn.ls.tree;
import java.util.HashSet;
import java.util.Set;
/**
* 部门
*
* @author tyg
*
*/
public class Department {
private Long id;
private Department parent;    //用于自连接.
private Set<Department> children = new HashSet<Department>();

private String name;
private String description;

public Long getId() {
return id;
}

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

public Department getParent() {
return parent;
}

public void setParent(Department parent) {
this.parent = parent;
}

public Set<Department> getChildren() {
return children;
}

public void setChildren(Set<Department> children) {
this.children = children;
}

public String getName() {
return name;
}

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

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Department other = (Department) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}

}
package cn.ls.tree;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import org.junit.Test;

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

/**
* 练习一:打印所有顶层部门及其子孙部门的信息(名称) 提示:假设有一个 打印部门树 的信息 的方法
*
* 要求打印如下效果:
*
* <pre>
* 市场部
* 市场1部
* 市场2部
* 21
* 22
* 开发部
* 开发1部
* 开发2部
* </pre>
*/
@Test
public void printAllDepts_1() {
List<Department> topList = findTopLevelDepartmentList();

for (Department top : topList) {
printTree(top);
}
}

/**
* 打印一颗树的信息
*
* @param top
*            顶点
*/
private void printTree(Department top) {
// 1个顶点
System.out.println(top.getName());

// n个子树
for (Department child : top.getChildren()) {
printTree(child);
}
}

/**
* 打印一堆树的信息
*
* @param topList
*/
private void printTreeList(Collection<Department> topList) {
for (Department top : topList) {
// 顶点
System.out.println(top.getName());
// 子树
printTreeList(top.getChildren());
}
}

/**
* 练习二:打印所有顶层部门及其子孙部门的信息(名称),用不同的缩进表示层次(使用全角空格)。<br>
* 子部门的名称前比上级部门多一个空格,最顶层部门的名字前没有空格。 提示:假设有一个打印部门集合中所有部门信息的方法
*
* 要求打印如下效果:
*
* <pre>
* ┠市场部
*     ┠市场1部
*     ┠市场2部
*     ┠21
*     ┠22
* ┠开发部
*     ┠开发1部
*     ┠开发2部
* </pre>
*/
@Test
public void printAllDepts_2() {
List<Department> topList = findTopLevelDepartmentList();

printTreeList_2(topList, "┣");
}

/**
* 打印一堆树的信息,显示的名称要有层次。名称前要比上级的名称前多一个空格。
*
* @param topList
*/
private void printTreeList_2(Collection<Department> topList, String prefix) {
for (Department top : topList) {
// 顶点
System.out.println(prefix + top.getName());
// 子树
printTreeList_2(top.getChildren(), " " + prefix);
}
}

/**
* 模拟的Department对象结构如下:
*
* <pre>
* ┠市场部
*      ┠市场1部
*      ┠市场2部
*       ┠21
*       ┠22
* ┠开发部
*      ┠开发1部
*      ┠开发2部
* </pre>
*
* @return 所有最顶层的部门的列表
*/
public static List<Department> findTopLevelDepartmentList() {
Department dept_1_1 = new Department();
dept_1_1.setId(new Long(11));
dept_1_1.setName("市场1部");

Department dept_1_2 = new Department();
dept_1_2.setId(new Long(12));
dept_1_2.setName("市场2部");

Department dept_1_2_1 = new Department();
dept_1_2_1.setId(new Long(121));
dept_1_2_1.setName("21");

Department dept_1_2_2 = new Department();
dept_1_2_2.setId(new Long(122));
dept_1_2_2.setName("22");

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("开发1部");

Department dept_2_2 = new Department();
dept_2_2.setId((new Long(22)));
dept_2_2.setName("开发2部");

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;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: