1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| public static List<Menu> parseMenuTree(List<Menu> list){ List<Menu> result = new ArrayList<Menu>(); for (Menu menu : list) { if(null == menu.getPid()) { result.add(menu); } } for (Menu parent : result) { parent = recursiveTree(parent, list); } return result; }
public static Menu recursiveTree(Menu parent, List<Menu> list) { for (Menu menu : list) { if(Objects.equals(parent.getId(),menu.getPid())) { menu = recursiveTree(menu, list); parent.getChildren().add(menu); } } return parent; }
|