For循环,这里的listMap必须在for循环之前new一下(循环外面new ArrayList对象),不然每次循环加入到list中,下次循环就把上次的数据清空了,最后存到list中是最后一条数据。

    PositionLog positionLog1 = new PositionLog("9901", "Cbot/CNY_11", "MARGIN_COM", new BigDecimal(1.5),

    new BigDecimal(100));
    PositionLog positionLog2 = new PositionLog("9901", "Cbot/CNY_12", "MARGIN_COM", new BigDecimal(2.5),
    new BigDecimal(2));

    List positionLogs = new ArrayList();
    positionLogs.add(positionLog1);
    positionLogs.add(positionLog2);

    List> listMap = null;
    for (PositionLog pLog : positionLogs) {
  listMap = new ArrayList>();
  // 这里的listMap必须要放到循环外层new一下ArrayList,不然每次循环加入到list中,下次循环new对象就把上次加入的数据给清空了的,最后存到list中的是最后一条数据。
  Map params = new HashMap();
  params.put("bankId", pLog.getBankId());
  params.put("productId", pLog.getProductId());
  params.put("instrumentId", pLog.getInstrumentId());
  params.put("nonBalPosition", pLog.getNonBalPosition().toString());
  params.put("balPosition", pLog.getBalPosition().toString());
  listMap.add(params);
    }

 

正确如下:

    List> listMap = new ArrayList>();
    for (PositionLog pLog : positionLogs) {
  Map params = new HashMap();
  params.put("bankId", pLog.getBankId());
  params.put("productId", pLog.getProductId());
  params.put("instrumentId", pLog.getInstrumentId());
  params.put("nonBalPosition", pLog.getNonBalPosition().toString());
  params.put("balPosition", pLog.getBalPosition().toString());
  listMap.add(params);
    }

    System.out.println(listMap.toString());

你可能感兴趣的:(web)