Java集合中的奇怪结果

时间:2013-10-11 13:02:07

标签: java

我从数据库中获取一些记录并添加HashMap<Integer, Object>。然后我将此HashMap添加到Vector<HashMap<Integer, Object>>。现在问题是什么时候。我正在打印Vector,我没有按照相同的广告订单获取记录...请帮忙。

public Vector<HashMap<Integer, Object>> executeQueryAsIntegerColumnNames(String aQuery, HashMap<String,String> conditions, String likeQuery){
    LOGGER.info("Query in Execute:"+aQuery);
    LOGGER.info("Query in Execute:"+conditions);
    LOGGER.info("Query in Execute:"+likeQuery);
    Vector <HashMap<Integer,Object>> result = null;

    Connection conn = connection.getMySQLConnection();
    String concond = this.getConditions(conditions);

    try {
        Statement stmt = conn.createStatement();    
        stmt = conn.createStatement();                     
        ResultSet rs = stmt.executeQuery(aQuery + concond);    
        ResultSetMetaData metaInfo = null;
        if(rs != null){
            metaInfo =rs.getMetaData();
        }
        while(rs != null && rs.next()){
            if(result == null){
                result = new Vector<HashMap<Integer, Object>>();
            }
            HashMap<Integer, Object> row = new HashMap<Integer, Object>();

            for(int i = 1 ; i <= metaInfo.getColumnCount(); i++){
                row.put(i, rs.getObject(i));
            }

            result.add(row);
        }
    }catch(Exception ex){
        LOGGER.error("executeQueryAsIntegerColumnNames : "+ex);
    }finally{
        try {
            conn.close();
        } catch (SQLException e) {
            LOGGER.error("Exception :",e);
        }                   
    }            
    LOGGER.info("Befor Returning to Caller : "+result.toString());    
    return result;
}

Vector支持插入订单???如果是,那么这是我的OutPut请看看

Befor返回来电:[{1 = mah0300537,2 = nabi hussain,3 = Mah03,4 = 05:50:00 PM,5 = 233346,6 = 0},{1 = cha0700003,2 = sita sharan ray,3 = cha07,4 = 05:50:00 PM,5 = 233347,6 = 2}]

Befor返回来电:[{1 = cha0700003,2 = sita sharan ray,3 = cha07,4 = 05:50:00 PM,5 = 233347,6 = 2},{1 = mah0300537,2 = nabi hussain,3 = Mah03,4 = 05:50:00 PM,5 = 233346,6 = 0}]

3 个答案:

答案 0 :(得分:8)

HashMap不维护广告订单。请改用LinkedHashMap

答案 1 :(得分:3)

来自HashMap javadoc:

  

此课程不保证地图的顺序;特别是,它不保证订单会随着时间的推移保持不变。

如果要保持插入元素在地图中的顺序,请使用LinkedHashMap。

答案 2 :(得分:1)

如果您必须维护订单,请使用 LinkedHashMap http://docs.oracle.com/javase/6/docs/api/java/util/LinkedHashMap.html