list集合生成树
来源:互联网 发布:五点共圆 知乎 编辑:程序博客网 时间:2024/06/07 12:48
public void selectbranchtree(){
ApplicationContext actx = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-dao.xml");
FyBranchMapper mapper = actx.getBean(FyBranchMapper.class);
List<BranchTree> list = mapper.selectbranchtree();
System.out.println(list);
System.out.println(list.get(0).getBbLevel());
List<BranchTree> children = createTree(list);
System.out.println(children);
}
public List<BranchTree> createTree(List<BranchTree> list) {
List<BranchTree> children = new ArrayList<BranchTree>();
// 当等级最高时,停止排序
for (BranchTree node : list) {
if ("0".equals(node.getBbLevel())) {
initChildren(node, list);
children.add(node);
}
}
return children;
}
public static void initChildren(BranchTree treeNode, List<BranchTree> list) {
List<BranchTree> children = new ArrayList<BranchTree>();
// 当等级最高时,停止排序
// 将最低级别的放到数组中
for (BranchTree node : list) {
int parentId = treeNode.getBbId();
if (parentId == node.getBbParentid()){
initChildren(node, list);
children.add(node);
}
}
treeNode.setChildren(children);
}
}
结果:
[BranchTree [bbId=1, bbParentid=0, bbName=总部, bbLevel=0, children=[]], BranchTree [bbId=2, bbParentid=1, bbName=区域, bbLevel=1, children=[]], BranchTree [bbId=3, bbParentid=2, bbName=中心路店, bbLevel=1, children=[]], BranchTree [bbId=4, bbParentid=2, bbName=共和新路店, bbLevel=3, children=[]], BranchTree [bbId=5, bbParentid=1, bbName=财务, bbLevel=2, children=[]], BranchTree [bbId=6, bbParentid=1, bbName=开发, bbLevel=3, children=[]]]
0
[BranchTree [bbId=1, bbParentid=0, bbName=总部, bbLevel=0, children=[BranchTree [bbId=2, bbParentid=1, bbName=区域, bbLevel=1, children=[BranchTree [bbId=3, bbParentid=2, bbName=中心路店, bbLevel=1, children=[]], BranchTree [bbId=4, bbParentid=2, bbName=共和新路店, bbLevel=3, children=[]]]], BranchTree [bbId=5, bbParentid=1, bbName=财务, bbLevel=2, children=[]], BranchTree [bbId=6, bbParentid=1, bbName=开发, bbLevel=3, children=[]]]]]
- list集合生成树
- 从list中取N个随机生成一个集合
- SpringMvc Ajax获取List集合动态生成表格
- list 集合
- List集合
- List集合
- List集合
- List集合
- List集合
- list 集合
- List集合
- 集合 --- List
- 集合(List)
- List集合
- 集合-List
- List集合
- list集合
- List集合
- stl之rope大法好及可持久化并查集用法
- svm+hog
- 实现xlistview+xutils展示数据之后点击item跳转页面显示图片缩放和放大
- JQ选择器大全追加1
- soj3343:Tower
- list集合生成树
- 前端学习资源网站
- JImmy的文档: 搭建集群版openshift origin-1.5.0
- 实现客户姓名添加和显示,修改
- C#读取txt文档并上传至数据库,将上传的数据逐行删除
- 硬盘分区表知识——详解硬盘MBR
- HDU
- Jenkins之定时构建
- 【HDU 1257】最少拦截系统(贪心)