不建議使用遞歸
//獲取父節(jié)點(diǎn)
List<Menu> collect = menus.stream().filter(m -> m.getPid() == 0).map(
(m) -> {
m.setChildList(getChildrens(m, menus));
return m;
}
).collect(Collectors.toList());
System.out.println("-------轉(zhuǎn)json輸出結(jié)果-------");
System.out.println(JSON.toJSON(collect));
}
/**
* 遞歸查詢子節(jié)點(diǎn)
*
* @param root 根節(jié)點(diǎn)
* @param all 所有節(jié)點(diǎn)
* @return 根節(jié)點(diǎn)信息
*/
private List<Menu> getChildrens(Menu root, List<Menu> all) {
List<Menu> children = all.stream().filter(m -> {
return Objects.equals(m.getPid(), root.getId());
}).map(
(m) -> {
m.setChildList(getChildrens(m, all));
return m;
}
).collect(Collectors.toList());
return children;
}