如果list中的对像的编号相同就把对像合并,并将数量相加

强烈推荐

分享一个大神的人工智能教程。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到人工智能的队伍中来!http://www.captainbed.net

个人项目中遇到了一个需求:
1.假设有产品中有很多零配件,很多产品中的零配件有通用件,也就是编号一样的零配件。
2.如果客户下订单,订单中有很多产品。
3.然后要根据这些订单中的产品的数量进行采购,如果他们中有通用件的话就需要把相同编号的数量相加,不然就会出问题的,如果一个产品用500个某零配件,另一个产品也用500个零配件。那么这个零配件就采购1000个才可以,但是在数据库中进行查询的话,按某一个产品进行查询的话就会显示只要500个。
4.所以我们需要进行订单中的产品相同的零配件进行相加,然后形成一个订单采购表。

所以需求实现的思路就出来了:

List list = new ArrayList<>();
		Map map = new HashMap<>();
		for(Object obj : list){
			if(map.containsKey(obj.getNo)){
				Object o = map.get("obj");
				o.setNum(o.getNum + obj.getNum);
				map.put(obj.getNo, o);
			}else{
				map.put(obj.getNo, obj);
			}
		}
 
  

具体例子:



//已经售出房源
		List soldHouse = new ArrayList<>();
		//库存房源
		List inventoryHouse = new ArrayList<>();
//查找所有的上架的房源
		String houseHql = "from House where projectId = '" + project.getProjectId() + "'";
		
		List houseList = baseDao.findByHql(houseHql);
for(House house : houseList){
			
				//查找已经签约的认购,签约代表房子已经售出
				String contractHql = "from ContractRecords where projectId = '" + project.getProjectId() + "'"
						+ " and recordStatus = 5 and houseNum = " + house.getHouseNum() + "";
				ContractRecords cr = (ContractRecords) baseDao.loadObject(contractHql);
				
				HouseData houseData = new HouseData();
				houseData.setBuildArea(house.getBuildArea());
				houseData.setSoldNum(1);
				if(cr == null){//库存
					inventoryHouse.add(houseData);
				}else{//售出
					soldHouse.add(houseData);
				}
		}

//售出房源过滤
		Map map2 = new HashMap<>();
		//库存房源过滤
		Map map3 = new HashMap<>();
//售出房源
		for(HouseData sh : soldHouse){
			if(map2.containsKey(sh.getBuildArea())){
				HouseData hd = new HouseData();
				hd.setBuildArea(sh.getBuildArea());
				Integer a = (Integer) map2.get(sh.getBuildArea());
				hd.setSoldNum(a + 1);
				map2.remove(sh.getBuildArea());
				map2.put(hd.getBuildArea(), hd.getSoldNum());
			}else{
				map2.put(sh.getBuildArea(), sh.getSoldNum());
			}
		}
		//库存房源
		for(HouseData ih : inventoryHouse){
			if(map3.containsKey(ih.getBuildArea())){
				HouseData hd = new HouseData();
				hd.setBuildArea(ih.getBuildArea());
				hd.setSoldNum((Integer)(map3.get(ih.getBuildArea())) + 1);
				map3.remove(ih.getBuildArea());
				map3.put(hd.getBuildArea(), hd.getSoldNum());
			}else{
				map3.put(ih.getBuildArea(), ih.getSoldNum());
			}
		}
		
		//清空集合
		soldHouse.clear();
		inventoryHouse.clear();
		
		Iterator i1 = map2.entrySet().iterator();
		Iterator i2 = map3.entrySet().iterator();
		
		while(i1.hasNext()){
			HouseData house = new HouseData();
			Entry entry = (Entry) i1.next();
			Object o = entry.getKey();
			String s = o.toString();
			Double d = Double.valueOf(s);
			house.setBuildArea(d);
			house.setSoldNum(Integer.parseInt(map2.get(d).toString()));
			soldHouse.add(house);
		}
		while(i2.hasNext()){
			HouseData house = new HouseData();
			Entry entry = (Entry) i2.next();
			Object o = entry.getKey();
			String s = o.toString();
			Double d = Double.valueOf(s);
			house.setBuildArea(d);
			house.setSoldNum(Integer.parseInt(map3.get(d).toString()));
			inventoryHouse.add(house);
		}
 		
		map.put("soldHouse", soldHouse);
		map.put("inventoryHouse", inventoryHouse);
		
		return map;

你可能感兴趣的:(list,map)