map 重复添加会被覆盖

首先最开始我的代码 :

@RequestMapping("/index")
	@ResponseBody
	public Object index() throws Exception{
	    PageData pd=this.getPageData();	
	    Map<String,Object> map=new HashMap<String,Object>();
	    Map<String,Object> data=new HashMap<String,Object>();
	    //List<PageData> goodsList=new ArrayList<PageData>();
	    List<PageData> banner=indexService.findBanner(pd);
	    List<PageData> channel= indexService.findchannel(pd);
	    List<PageData> newGoodsList= goodsService.findnewgoods(pd);
	    List<PageData> hotGoodsList= goodsService.findhotgoods(pd);
	    List<PageData> brandList= goodsService.findbrand(pd);
	    List<PageData> topicList= topicService.findtopic(pd);
	    List<PageData> categoryList=  categoryService.findCategory(pd);
	    for(int i=0;i<categoryList.size();i++){
	       long id= (Long) categoryList.get(i).get("id");
	       pd.put("id", id); 
	       List<PageData> goodsList=  goodsService.findgoodsbyCategory(pd); 
	       //goodsList.addAll(goods);
	       data.put("goodsList",goodsList);
	    }
	    //data.put("goodsList",goodsList);
	    data.put("categoryList",categoryList);
	    data.put("topicList",topicList);
	    data.put("brandList",brandList);
	    data.put("banner",banner);
	    data.put("channel", channel);
	    data.put("newGoodsList",newGoodsList);
	    data.put("hotGoodsList",hotGoodsList);
		map.put("data", data);
		map.put("errno", 0);
		return map;
	}

 

 

修改之后:

@RequestMapping("/index")
	@ResponseBody
	public Object index() throws Exception{
	    PageData pd=this.getPageData();	
	    Map<String,Object> map=new HashMap<String,Object>();
	    Map<String,Object> data=new HashMap<String,Object>();
	    List<PageData> goodsList=new ArrayList<PageData>();
	    List<PageData> banner=indexService.findBanner(pd);
	    List<PageData> channel= indexService.findchannel(pd);
	    List<PageData> newGoodsList= goodsService.findnewgoods(pd);
	    List<PageData> hotGoodsList= goodsService.findhotgoods(pd);
	    List<PageData> brandList= goodsService.findbrand(pd);
	    List<PageData> topicList= topicService.findtopic(pd);
	    List<PageData> categoryList=  categoryService.findCategory(pd);
	    for(int i=0;i<categoryList.size();i++){
	       long id= (Long) categoryList.get(i).get("id");
	       pd.put("id", id); 
	       List<PageData> goods=  goodsService.findgoodsbyCategory(pd); 
	      goodsList.addAll(goods);
	    }
	    data.put("goodsList",goodsList);
	    data.put("categoryList",categoryList);
	    data.put("topicList",topicList);
	    data.put("brandList",brandList);
	    data.put("banner",banner);
	    data.put("channel", channel);
	    data.put("newGoodsList",newGoodsList);
	    data.put("hotGoodsList",hotGoodsList);
		map.put("data", data);
		map.put("errno", 0);
		return map;
	}
List<PageData> goodsList=new ArrayList<PageData>();
	    List<PageData> banner=indexService.findBanner(pd);
	    List<PageData> channel= indexService.findchannel(pd);
	    List<PageData> newGoodsList= goodsService.findnewgoods(pd);
	    List<PageData> hotGoodsList= goodsService.findhotgoods(pd);
	    List<PageData> brandList= goodsService.findbrand(pd);
	    List<PageData> topicList= topicService.findtopic(pd);
	    List<PageData> categoryList=  categoryService.findCategory(pd);
	    for(int i=0;i<categoryList.size();i++){
	       long id= (Long) categoryList.get(i).get("id");
	       pd.put("id", id); 
	       List<PageData> goods=  goodsService.findgoodsbyCategory(pd); 
	      goodsList.addAll(goods);
	    }
	    data.put("goodsList",goodsList);
	    data.put("categoryList",categoryList);
	    data.put("topicList",topicList);
	    data.put("brandList",brandList);
	    data.put("banner",banner);
	    data.put("channel", channel);
	    data.put("newGoodsList",newGoodsList);
	    data.put("hotGoodsList",hotGoodsList);
		map.put("data", data);
		map.put("errno", 0);
		return map;
	}

欢迎访问我的网站

 

 


这样添加的数据就不会被覆盖了

欢迎 ⭐️⭐️⭐️⭐️⭐️⭐️⭐️⭐️⭐️⭐️⭐️⭐️⭐️⭐️⭐️⭐️⭐️⭐️

< END >

程序员NBA

一个有故事的程序员

在这里插入图片描述

或者加我微信1414200300一起讨论java技术,有什么问题群里人也可以帮你一起解决

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值