package app.util;
import java.util.ArrayList;
import java.util.List;
import com.alibaba.fastjson.JSON;
public class Menu {
private Integer pid;
private String menuName;
private Integer parentId;
private Integer position;
private List
public Integer getPid() {
return pid;
}
public void setPid(Integer pid) {
this.pid = pid;
}
public String getMenuName() {
return menuName;
}
public void setMenuName(String menuName) {
this.menuName = menuName;
}
public Integer getParentId() {
return parentId;
}
public void setParentId(Integer parentId) {
this.parentId = parentId;
}
public Integer getPosition() {
return position;
}
public void setPosition(Integer position) {
this.position = position;
}
public List
return childMenu;
}
public void setChildMenu(List
this.childMenu = childMenu;
}
public Menu(Integer pid, String menuName, Integer parentId, Integer position) {
super();
this.pid = pid;
this.menuName = menuName;
this.parentId = parentId;
this.position = position;
}
public Menu() {
super();
}
public static void main(String[] args) {
List
List
Menu menu1=new Menu(1, "父级1", 0, 1);
Menu menu2=new Menu(2, "父级2", 0, 2);
Menu menu1_1=new Menu(3, "子级1_1", 1, 1);
Menu menu1_2=new Menu(4, "子级1_2", 1, 2);
Menu menu1_2_1=new Menu(5, "子级1_2_1", 4, 2);
list.add(menu1);
list.add(menu2);
list.add(menu1_1);
list.add(menu1_2);
list.add(menu1_2_1);
List
String trssJson=JSON.toJSONString(listTree);
System.out.println(trssJson);
}
public static List
List
for (Menu menu : list) {
int menuId = menu.getPid();
int pid = menu.getParentId();
if (parentId == pid) {
List
menu.setChildMenu(menuLists);
menus.add(menu);
}
}
return menus;
}
}
原文:https://www.cnblogs.com/-flq/p/10208165.html