springboot 实现无限级 菜单的几种写法

递归:

entity层

@Data
public class Tree {
	    private Integer id;
	    private String name;
	    private Integer pid;
	    private List<Tree> treeList =new ArrayList<>();

}

TreeServiceImpl 层:

public List<Tree> getTree() {
        List<Tree> tree = treeDao.getTree();//返回所有父级
        List<Tree> allTree = treeDao.findAllTree();//返回所有数据
        for (Tree list : tree) {
            list.setTreeList(treeList(allTree,list.getId()));
        }
       return tree;
    }
private static List<Tree> treeList(List<Tree> list, Integer rootId) {
    List<Tree> newList = new ArrayList<>();
    for (Tree tree : list) {
        if (tree.getPid() == rootId.intValue()) {
            List<Tree> tempList = treeList(list, tree.getId());
            tree.setTreeList(tempList);
            newList.add(tree);
        }
    }
    return newList;
}

Map实现

public List<Tree> getTree() {
		//返回
        List<Tree> allTree = treeDao.findAllTree();
        Map<Long,Tree> map = new HashMap<>();
          //ID 为 key 存储到map 中
        for (Tree tree:allTree) {
            map.put(tree.getId().longValue(),tree);
        }
      List<Tree> treeList=new  ArrayList<Tree>();
        for (Tree tree:allTree) {
        //子集ID返回对象,有则添加。
            Tree tree1 = map.get(tree.getPid().longValue());
            if(tree1 != null){
                tree1.getTreeList().add(tree);
            }else {
                treeList.add(tree);
            }
        }
        return treeList;
    }
  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
单例模式一般有以下几种写法: 1. 懒汉式单例模式:在第一次使用时才创建实例,线程不安全,可以使用synchronized关键字进行同步,但是会影响性能。 ```java public class Singleton { private static Singleton instance = null; private Singleton() {} public static Singleton getInstance() { if (instance == null) { instance = new Singleton(); } return instance; } } ``` 2. 饿汉式单例模式:在类加载时就创建实例,线程安全,但是无法进行延迟加载。 ```java public class Singleton { private static Singleton instance = new Singleton(); private Singleton() {} public static Singleton getInstance() { return instance; } } ``` 3. 双重校验锁单例模式:使用双重校验锁机制,既能保证线程安全,又能够进行延迟加载。 ```java public class Singleton { private static volatile Singleton instance = null; private Singleton() {} public static Singleton getInstance() { if (instance == null) { synchronized (Singleton.class) { if (instance == null) { instance = new Singleton(); } } } return instance; } } ``` 4. 静态内部类单例模式:使用静态内部类实现单例模式,既能保证线程安全,又能够进行延迟加载。 ```java public class Singleton { private Singleton() {} private static class SingletonHolder { private static final Singleton INSTANCE = new Singleton(); } public static Singleton getInstance() { return SingletonHolder.INSTANCE; } } ``` 以上是几种常见的单例模式写法。需要注意的是,单例模式在多线程环境下需要特别小心,需要保证线程安全。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值