1

i am getting some records from database and adding in HashMap<Integer, Object> .Then i am adding this HashMap to a Vector<HashMap<Integer, Object>>. Now problem is when. I am printing the Vector, I am not getting the records in the same insertion order ... Please help.

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; } 

Does Vector support insertion Order??? IF YES then This is my OutPut Please have a look

Befor Returning to Caller : [{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 Returning to Caller : [{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}]

11
  • 5
    HashMap doesn't preserve order. Use a LinkedHashMap instead. Commented Oct 11, 2013 at 13:03
  • may be , if your quey has no order by thne every time it will fetch data in different order , hence while you are adding featched values in hasmap the order is different may be , therefor every single time the prnt order is different Commented Oct 11, 2013 at 13:08
  • 1
    But why Vector<HashMap<Integer,ObjecT>> ? I think simple ArrayList is more than enough for your code. Commented Oct 11, 2013 at 13:09
  • @vels4j Vector is synchronized, so using Vector instead of ArrayList is justifiable in certain cases. Commented Oct 11, 2013 at 13:12
  • @vels4j ArrayList is not synchronized thats why i am using Vector.Thanks! Commented Oct 11, 2013 at 13:14

3 Answers 3

8

HashMap doesn't maintain insertion order. Use LinkedHashMap instead.

Sign up to request clarification or add additional context in comments.

Comments

3

From HashMap javadoc:

This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.

Use LinkedHashMap if you want to keep the order of inserted elements in a map.

1 Comment

1

If you have to maintain order use LinkedHashMap . http://docs.oracle.com/javase/6/docs/api/java/util/LinkedHashMap.html

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.