优化 封装Map集合

这篇博客主要介绍了如何优化Java中Map集合的使用,通过示例展示了如何根据数据库查询结果构建多级菜单数据结构。同时,提供了删除数据的方法,针对不同级别的菜单项执行不同的删除策略。内容涵盖了Map的高效操作和数据层级结构的构建。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/优化封装Map集合
    public Map<Integer, List<ItemCat>> getMap() {
            Map<Integer,List<ItemCat>>map=new HashMap<>();
            //查询数据库的所有的记录
        List<ItemCat> list = itemCatMapper.selectList(null);
        for (ItemCat itemCat:list){
            int key=itemCat.getParentId();
            if (map.containsKey(key)){//true 有数据 将自己追加
                map.get(key).add(itemCat);

            }else{//false 没有数据 准备新集合,添加数据
                List<ItemCat> childrenList=new ArrayList<>();
                childrenList.add(itemCat);
                map.put(key,childrenList);
            }
        }
        return map;
    }
 @Override
    public List<ItemCat> findItemCatList( Integer level) {
        long l = System.currentTimeMillis();
        //获取数据信息
        Map<Integer,List<ItemCat>> map =getMap();
        if (level==1){//只获取一级菜单数据
            return map.get(0);
        }
        if (level==2){
            return gettwoList(map);
        }
        List<ItemCat> list=getThreeList(map);
        long l1 = System.currentTimeMillis();
        System.out.println("运行时间"+(l1-l)+"毫秒");
        //获取1-2-3级数据
        return list;

 

private List<ItemCat> gettwoList(Map<Integer, List<ItemCat>> map) {
        //获取一级数据
        List<ItemCat> oneList=map.get(0);
        for (ItemCat oneItemCat:oneList){
            //根据一级ID查询二级集合
            int key=oneItemCat.getId();
            List<ItemCat> twoList=map.get(key);
            //将二级数据封装到一级中
            oneItemCat.setChildren(twoList);
        }
        return oneList;
    }
  private List<ItemCat> getThreeList(Map<Integer, List<ItemCat>> map) {

       //1.获取以及和二级的数据
       List<ItemCat> oneList=gettwoList(map);
       for (ItemCat oneItemCat:oneList){
           List<ItemCat> twoList=oneItemCat.getChildren();
           if (twoList==null){
               //说明 该下级没有二级数据,所以跳过本次循环进入下一次
               continue;
           }
           for (ItemCat twoItemCat:twoList){
               //查询三级数据,使用二级ID
               int key =twoItemCat.getId();
               List<ItemCat> threeList = map.get(key);
               twoItemCat.setChildren(threeList);
           }
       }
       return oneList;
    }

删除数据

 @Override
    @Transactional
    public void deleteItemCat(ItemCat itemCat) {
        if (itemCat.getLevel()==3){
           itemCatMapper.deleteById(itemCat.getId());
           return;
        }
        if (itemCat.getLevel()==2){
            int twoId=itemCat.getId();
            QueryWrapper<ItemCat> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("parent_id", twoId);
            itemCatMapper.delete(queryWrapper);
            itemCatMapper.deleteById(twoId);
            return;
        }

            int oneId=itemCat.getId();
        QueryWrapper<ItemCat> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", oneId);
            List twoList = itemCatMapper.selectObjs(queryWrapper);
            if (twoList.size()==0){
                itemCatMapper.deleteById(oneId);

            }else{
                queryWrapper.clear();
                queryWrapper.in("parent_id", twoList)
                        .or()
                        .in("id", twoList)
                        .or()
                        .eq("id", oneId);
                itemCatMapper.deleteById(queryWrapper);
            }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值