java递归
实体类
@Data @AllArgsConstructor @NoArgsConstructor public class Menu { private String menu_id; private String parent_id; private String menu_name; private String menu_url; private String menu_sort; private String product_id; private List<Menu> children; private String url; }
树结构递归
private List<Menu> createTree(String pid, List<Menu> menus) { List<Menu> treeMenu = new ArrayList<>(); for (Menu menu : menus) { if((pid).equals(menu.getParent_id())){ if(("".equals(pid))){ menu.setParent_id(null); } menu.setChildren(createTree(menu.getMenu_id(), menus)); treeMenu.add(menu); } } return treeMenu; }
调用
createTree("", list)