树形list(菜单树)递归遍历list

有些时候为了方便jsp展示菜单,我们一般将查询出的菜单直接遍历重组成树形,再到前端展示。废话不多说,直接贴代码。

首先要写个entity类

package com.guo.test;

public class TestEntity {
	private int id;
	private String name;
	private int parentId;
	private int level;

	public TestEntity(int id, String name, int parentId,int level) {
		super();
		this.id = id;
		this.name = name;
		this.parentId = parentId;
		this.level = level;
	}

	public int getId() {
		return id;
	}

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

	public String getName() {
		return name;
	}

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

	public int getParentId() {
		return parentId;
	}

	public void setParentId(int parentId) {
		this.parentId = parentId;
	}

	public int getLevel() {
		return level;
	}

	public void setLevel(int level) {
		this.level = level;
	}

	@Override
	public String toString() {
		return "Location [id=" + id + ", name=" + name + ", parentId=" + parentId + ", level=" + level + "]";
	}

}
然后就是main方法了

package com.guo.test;

import java.util.ArrayList;
import java.util.List;

public class Test {
	// 查询数据库菜单表中所有数据(这里模拟一个菜单表的所有数据)
	static int times=0;
	static List<TestEntity> data = new ArrayList<TestEntity>();
	static {
		TestEntity l = new TestEntity(1, "北京市", 0,1);
			
			TestEntity l2 = new TestEntity(3, "朝阳区", 1,2);
				TestEntity l4 = new TestEntity(5, "大郊亭中街", 3,3);
					TestEntity l11 = new TestEntity(12, "二号院", 5,4);
						TestEntity l12 = new TestEntity(13, "三号楼", 12,5);
						TestEntity l13 = new TestEntity(14, "四号楼", 12,5);
				TestEntity l5 = new TestEntity(6, "大望路", 3,3);
				TestEntity l6 = new TestEntity(7, "南磨房路", 3,3);
			TestEntity l3 = new TestEntity(4, "海淀区", 1,2);
				TestEntity l7 = new TestEntity(8, "唐家岭路", 4,3);
				TestEntity l8 = new TestEntity(9, "上地路", 4,3);
		
		TestEntity l1 = new TestEntity(2, "天津市", 0,1);
			TestEntity l9 = new TestEntity(10, "红桥区", 2,2);
			TestEntity l10 = new TestEntity(11, "北辰区", 2,2);
		
		data.add(l2);
		data.add(l);
		data.add(l1);
		data.add(l3);
		data.add(l10);
		data.add(l9);
		data.add(l5);
		data.add(l4);
		data.add(l7);
		data.add(l6);
		data.add(l8);
		data.add(l12);
		data.add(l11);
		data.add(l13);
	}
	
	public static void main(String[] args) {
		List<TestEntity> resultList = new ArrayList<>();
		System.out.println("查询前集合大小:"+data.size());
		long start = System.currentTimeMillis();
		for (TestEntity testEntity : data) {
			times++;
			if (testEntity.getParentId()==0) {//父级菜单开始添加
				resultList.add(testEntity);
				if (ifChilds(data, testEntity.getId())) {//存在子集
					List<TestEntity> childs = new ArrayList<>();
					childs = getChildList(data,testEntity.getId(),childs);
					resultList.addAll(childs);
				}
			}
		}
		long end = System.currentTimeMillis();
		System.out.println("查询后集合大小:"+resultList.size());
		System.out.println("查询次数:"+times+"次");
		System.out.println("查询耗时:"+(end-start)+"毫秒");
		
		for (TestEntity testEntity : resultList) {
			String s = "";
			for (int i = 1; i < testEntity.getLevel(); i++) {
				s = s + "\t";
			}
			System.out.println(s+"I____"+testEntity.getName());
		}
		
	}
	
	//获取父id下的子集合
	private static List<TestEntity> getChildList(List<TestEntity> list,int pId,List<TestEntity> reList) {
		for (TestEntity testEntity : list) {
			times++;
			if (testEntity.getParentId()==pId) {//查询下级菜单
				reList.add(testEntity);
				if (ifChilds(list, testEntity.getId())) {
					getChildList(list, testEntity.getId(), reList);
				}
			}
		}
		return reList;
	}
	
	//判断是否存在子集
	private static boolean ifChilds(List<TestEntity> list,int pId) {
		boolean flag = false;
		for (TestEntity testEntity : list) {
			times++;
			if (testEntity.getParentId()==pId) {
				flag=true;
				break;
			}
		}
		return flag;
	}

}

好了,test一下,排列出了我们想要的树形list,在页面直接遍历list就可以了,就不用再去写复杂的展示了。

  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值