递归树结构封装

/**
* 递归转化树形菜单
*/
private List<Map<String, Object>> getMenuTree(List<Authorities> authorities, Integer parentId) {
List<Map<String, Object>> list = new ArrayList<>();
for (int i = 0; i < authorities.size(); i++) {
Authorities temp = authorities.get(i);
//第一次temp.getParentId()=-1 parentId=-1 判断进入重新递归方法
//第二次temp.getParentId()=-1 parentId=1 不递归不进判断
//第二次temp.getParentId()=2 parentId=1 不递归不进判断
if (temp.getIsMenu() == 0 && parentId == temp.getParentId()) {
Map<String, Object> map = new HashMap<>();
map.put("menuName", temp.getAuthorityName());
map.put("menuIcon", temp.getMenuIcon());
map.put("menuUrl", StringUtil.isBlank(temp.getMenuUrl()) ? "javascript:;" : temp.getMenuUrl());
map.put("subMenus", getMenuTree(authorities, authorities.get(i).getAuthorityId()));
list.add(map);
}
}
return list;
}

猜你喜欢

转载自www.cnblogs.com/java-llp/p/10962908.html