public class MenuTree1 { /** * 构建结束的树 */ private static List<Menu> menuList ; /** * 构建树 * @param menuId 树的根节点id 0:代表顶级节点 * @param nodeList 要构建的数据 * @return */ public static List<Menu> tree3(Integer menuId, List<MyMenu> nodeList){ menuList = new ArrayList<Menu>(); Menu menu1=new Menu(); menu1.setMenuId(menuId); getNode3(menu1,nodeList); return menuList; } /** * 使用递归构建树 * @param menu1 * @param nodeList */ public static void getNode3(Menu menu1, List<MyMenu> nodeList){ Integer menuId =menu1.getMenuId(); for (MyMenu layer : nodeList) { if(menuId.toString().equals(layer.getParentId().toString())){ Integer menuId1=layer.getMenuId(); Menu menu=new Menu(); menu.setMenuId(menuId1); menu.setMenuName(layer.getMenuName()); menu.setParentId(layer.getParentId()); if (menuId.toString().equals("0")){ menuList.add(menu); }else { menu1.getChildren().add(menu); } getNode3(menu,nodeList); } } } } @Data public class MyMenu extends BaseEntity{ private static final long serialVersionUID = -6525908145032868815L; @TableId private Integer menuId; private Integer parentId; private String menuName; private String icon; private Integer type; private String url; private String permission; private Integer sort; } @Data public class Menu { private List<Menu> children=new ArrayList<>(); private Integer parentId; private Integer menuId; private String menuName; private Integer value; private String label; }
标签:Java,递归,menuId,menu,private,menu1,构建,nodeList,Integer From: https://www.cnblogs.com/RedOrange/p/17091617.html