递归树结构封装

 

/**
* 递归转化树形菜单
*/
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;
}

 

递归树结构封装

 递归树结构封装递归树结构封装

 

递归树结构封装递归树结构封装

 

public class Sheng {

private String name;

private int id;

private Integer pid;

private static List<Sheng> shenList;
private List<Shi> shiList;
private List<Xian> xianList;
static {
shenList=new ArrayList() {{
add(new Sheng("河南省",1,0));
add(new Sheng("郑州市",2,1));
add(new Sheng("开封市",3,1));
add(new Sheng("洛阳市",4,1));
add(new Sheng("洛阳市-1",5,4));
add(new Sheng("洛阳市-2",6,4));
add(new Sheng("洛阳市-3",7,4));
add(new Sheng("洛阳市-4",8,4));
}};
}

public static List<Map<String, Object>> bulidTree(List<Sheng> sheng, int pid_) {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
for (Sheng item : sheng) {
Sheng shen = item;
//第一次temp.getParentId()=-1 parentId=-1 判断进入重新递归方法
//第二次temp.getParentId()=-1 parentId=1 不递归不进判断
//第二次temp.getParentId()=2 parentId=1 不递归不进判断
if (pid_ == shen.getPid()) {
Map<String, Object> map = new HashMap<>();
map.put("menuName", shen.getName());
map.put("menuIcon", "icon");
map.put("menuUrl", "login/test");
map.put("subMenus", bulidTree(sheng, shen.getId()));
list.add(map);
}
}
return list;
}

public static void main(String[] args) {
Sheng sheng = new Sheng();
System.out.println(bulidTree(sheng.getShenList(), 0));
}
}

https://blog.csdn.net/qq_36476972/article/details/75089927

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-16
  • 2021-11-22
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-09-26
  • 2021-07-31
  • 2022-12-23
  • 2021-06-15
  • 2021-11-09
  • 2021-06-29
  • 2022-12-23
相关资源
相似解决方案