Live2D

Java的树形结构遍历

定义 Menu

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
 
import java.util.List;
 
/**
 * @ClassName Menu
 * @Description TODO
 * @Author YZJ
 * @Date 2022/11/27 20:31
 **/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
public class Menu {
 
    private Long id;
    //菜单名称
    private String menuName;
    //父菜单ID
    private Long parentId;
 
    private List<Menu> children;
}

生成树形结构

public static void main(String[] args) {
        Menu menu = new Menu(0L, "国家", -1L, null);
        Menu menu1 = new Menu(1L, "中国", 0L, null);
        Menu menu2 = new Menu(2L, "日本", 0L, null);
        Menu menu3 = new Menu(3L, "美国", 0L, null);
 
        Menu menu4 = new Menu(4L, "北京", 1L, null);
        Menu menu5 = new Menu(5L, "上海", 1L, null);
        Menu menu6 = new Menu(6L, "武汉", 1L, null);
 
        Menu menu7 = new Menu(7L, "东京", 2L, null);
        Menu menu8 = new Menu(8L, "大阪", 2L, null);
 
        Menu menu9 = new Menu(9L, "华盛顿", 3L, null);
        Menu menu10 = new Menu(10L, "纽约", 3L, null);
        Menu menu11 = new Menu(11L, "加州", 3L, null);
 
        Menu menu12 = new Menu(12L, "洪山区", 6L, null);
        Menu menu13 = new Menu(13L, "武昌区", 6L, null);
        Menu menu14 = new Menu(14L, "汉阳区", 6L, null);
 
        List<Menu> menus = new ArrayList<>();
        menus.add(menu);
        menus.add(menu1);
        menus.add(menu2);
        menus.add(menu3);
        menus.add(menu4);
        menus.add(menu5);
        menus.add(menu6);
        menus.add(menu7);
        menus.add(menu8);
        menus.add(menu9);
        menus.add(menu10);
        menus.add(menu11);
        menus.add(menu12);
        menus.add(menu13);
        menus.add(menu14);
 
        // 从 0 开始构建整个树
        long parentId = 0L;
 
        System.out.println(JSON.toJSONString(builderMenuTree(menus, parentId)));
    }
    private static List<Menu> builderMenuTree(List<Menu> menus, long parentId) {
        return menus.stream()
                .filter(menu -> menu.getParentId() != null && menu.getParentId().equals(parentId))
                .map(menu -> menu.setChildren(getChildren(menu, menus)))
                .collect(Collectors.toList());
    }
 
    private static List<Menu> getChildren(Menu menu, List<Menu> menus) {
        return menus.stream()
                .filter(m ->  menu.getParentId() != null && m.getParentId().equals(menu.getId()))
                .map(m -> m.setChildren(getChildren(m,menus)))
                .collect(Collectors.toList());
    }

输出

[
    {
        "children":[
            {
                "children":[
 
                ],
                "id":4,
                "menuName":"北京",
                "parentId":1
            },
            {
                "children":[
 
                ],
                "id":5,
                "menuName":"上海",
                "parentId":1
            },
            Object{...}
        ],
        "id":1,
        "menuName":"中国",
        "parentId":0
    },
    {
        "children":Array[2],
        "id":2,
        "menuName":"日本",
        "parentId":0
    },
    {
        "children":Array[3],
        "id":3,
        "menuName":"美国",
        "parentId":0
    }
]

 

posted @ 2023-02-20 15:27  键盘命  阅读(210)  评论(0编辑  收藏  举报