Map的使用,值得参考的代码

@Override
    public ServerResponse<List<WebAppDownMenu>> getWebAppDownMenuList(String position) {
        // 1.根据position查找所有下载菜单
        List<WebAppDownMenu> menuList = webAppDownMenuMapper.selectCategoryByPosition(position);
        // 1.1 list<menuList>转换为map<type,menu>
        Map<String, WebAppDownMenu> type2menu =new HashMap<String,WebAppDownMenu>();
        for(WebAppDownMenu menu : menuList){
            type2menu.put(menu.getType(),menu);
        }

        // 2.遍历一遍结果,得到总的type列表[1,2,3...]、category列表[1,10]以及category下的type列表
        // key是type,value是categoryId
        Map<String,String> type2category = new HashMap<String,String>();
        // key是categoryId,value是<展示位置, type>
        Map<String, Map<String, String>> categoryMap = new TreeMap<String, Map<String, String>>();
        // key是categoryId,value是categoryName
        Map<String, String> category2name = new HashMap<String, String>();
        for(WebAppDownMenu menu : menuList){
            type2category.put( menu.getType(), menu.getCategoryId() );
            // 先用categoryId占位
            categoryMap.put(menu.getCategoryId() , new HashMap<String, String>() );
            category2name.put(menu.getCategoryId(), menu.getCategoryName() );
        }
        List<String> typeList = new ArrayList<String>();
        for(Map.Entry<String, String> entry : type2category.entrySet()){
            String type = entry.getKey();
            String categoryId = entry.getValue();
            typeList.add(type);
            categoryMap.get(categoryId).put( type, type );
        }

        //3、用总的type列表[1,2,3,..]查询软件详情web_appdown_control,形成map<type, appInfo>
        String typeStr = new String();
        String ignoreTypeStr = new String();
        for (String type : typeList) {
            if (typeStr.length() > 0) {
                typeStr +=",";
            }
            typeStr += "'" + type + "'";
        }
        // 打印typeStr
        logger.info("typeStr:" + typeStr);
        Map<String, Object> tmpmap = new HashMap<String, Object>();
        tmpmap.put("type", typeStr);
        List<WebAppDownControl> webAppDownControls = webAppDownControlMapper.getPresentVersionEntityByType(tmpmap);
        // list<app>转换为map<type, app>
        Map<String, WebAppDownControl> type2app = new HashMap<String, WebAppDownControl>();
        for (WebAppDownControl webAppDownControl : webAppDownControls) {
            type2app.put(webAppDownControl.getType(), webAppDownControl);
            String jsonList = webAppDownControl.getDownInfoJson();
            JSONArray jsonArray = JSON.parseArray(jsonList);
            //downInfo字段,json解析后的接收对象list声明
            List<AppDownInfo> downInfos = new ArrayList<>();
            for(int j = 0; j < jsonArray.size();j++){
                JSONObject jsonObject = jsonArray.getJSONObject(j);
                //获取json串中的key和value
                String appType = jsonObject.getString("type");
                String appUrl = jsonObject.getString("url");
                //实例化json解析后的接收对象AppDownInfo
                AppDownInfo appDownInfo = new AppDownInfo();
                appDownInfo.setType(appType);
                appDownInfo.setUrl(appUrl);
                //每封装一个AppDownInfo对象后就add进一个list里
                downInfos.add(appDownInfo);
            }
            webAppDownControl.setDownInfo(downInfos);
        }

        // 4、组合返回
        List<WebAppDownMenu> rspList = new ArrayList<>();
        for(Map.Entry<String, Map<String, String> > entry : categoryMap.entrySet()){
            typeStr = "";
            ignoreTypeStr = "";
            String categoryId = entry.getKey();
            String categoryName =category2name.get(categoryId);
            Map<String, String> type2display = entry.getValue();
            if(type2display == null){
                logger.info("categoryId =" + categoryId + ";categoryName =" + categoryName +"type2display is null");
                continue;
            }
            WebAppDownMenu menus = new WebAppDownMenu();
            menus.setPosition(position);
            menus.setCategoryId(categoryId);
            menus.setCategoryName(category2name.get(categoryId));
            List<WebAppDownControl> controlList = new ArrayList<>();
            for(Map.Entry<String, String> e : type2display.entrySet()){
                String type = e.getKey();
                WebAppDownControl control = type2app.get(type);
                if(control != null){
                    controlList.add(type2app.get(type));
                    typeStr += type + ",";
                }else{
                    ignoreTypeStr += type + ",";
                }
            }
            // 打印categoryId和对应的typeStr以及ignoreTypeStr
            logger.info("categoryId:" + categoryId + ";typeStr:" + typeStr + ";ignoreTypeStr:" + ignoreTypeStr);
            menus.setAppList(controlList);
            rspList.add(menus);
        }
        return ServerResponse.createBySuccess(rspList);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值