递归遍历树状结构优雅实现

cnblogs 2024-08-07 15:09:00 阅读 57

递归遍历树状结构优雅实现

实体:

<code>@Data

@Builder

public class Menu {

private Integer id;

private String name;

private Integer parentId;

private List<Menu> childrenList;

public Menu(Integer id, String name, Integer parentId) {

this.id = id;

this.name = name;

this.parentId = parentId;

}

public Menu(Integer id, String name, Integer parentId, List<Menu> childrenList) {

this.id = id;

this.name = name;

this.parentId = parentId;

this.childrenList = childrenList;

}

}

实现:

@SpringBootTest

public class RecursionTest {

@Test

public void recursion() {

List<Menu> menus = Arrays.asList(

new Menu(1, "根节点", 0),

new Menu(2, "子节点1", 1),

new Menu(3, "子节点1.1", 2),

new Menu(4, "子节点1.2", 2),

new Menu(5, "根节点1.3", 2),

new Menu(6, "根节点2", 1),

new Menu(7, "根节点2.1", 6),

new Menu(8, "根节点2.2", 6),

new Menu(9, "根节点2.2.1", 7),

new Menu(10, "根节点2.2.2", 7),

new Menu(11, "根节点3", 1),

new Menu(12, "根节点3.1", 11));

// 获取父节点

List<Menu> collect = menus.stream()

.filter(m -> m.getParentId() == 0)

.peek((m) -> m.setChildrenList(getChildren(m, menus)))

.collect(Collectors.toList());

System.out.println(JSONUtil.toJsonStr(collect));

}

private List<Menu> getChildren(Menu root, List<Menu> all) {

return all.stream()

.filter(m -> Objects.equals(m.getParentId(), root.getId()))

.peek(m -> m.setChildrenList(getChildren(m, all)))

.collect(Collectors.toList());

}

}



声明

本文内容仅代表作者观点,或转载于其他网站,本站不以此文作为商业用途
如有涉及侵权,请联系本站进行删除
转载本站原创文章,请注明来源及作者。