日期:2014-05-20 浏览次数:20770 次
List a = new ArrayList(); Map b = new HashMap(); b.put(1, 1); b.put(2,b); List list = (List)b.get(2);//取得集合a for(Object o : list){//循环集合a }
------解决方案--------------------
就是循环嵌套
服务器端处理
List<xxx> serverList = ...; //假设系统商品库集合,xxx是商品类 //接收处理 //假设用Object序列化传输 new ObjectInputStream ois = new ObjectInputStream(socket.getInputStream()); Map<Integer, Object> map = (Map<Integer, Object>)ois.readObject(); List<xxx> clientList = (List<xxx>)map.get(2); Map<Integer, xxx> result = new HashMap<Integer, xxx>(); //返回给客户端 for (xxx a : clientList) { for (xxx b : serverList) { if (a.getId() == b.getId()) {//客户端和系统商品库集合id相同 b.setAmount(b.getAmout() + a.getAmount()); //系统商品集合数量修改 //....其他修改 } } if (!result.containsKey(a.getId)) { result.put(a.getId(), new xxx()); } xxx b = result.get(a.getId()); b.setAmout(b.getAmout() + a.getAmout()); //返回给客户端的清单合计 //...其他合计 }
------解决方案--------------------
就是个循环嵌套,有什么不能遍历的,赞成5楼!