0

I have the following code:

    ResultSet rs = DB.doQuery("SELECT username,register_ip FROM users ORDER BY joined DESC LIMIT 10");
    Map<String,String> botInfo = new HashMap<String, String>();
    List<Map<String , String>> myMap  = new ArrayList<Map<String,String>>();
    int c = 0;

    while(rs.next()) {
        botInfo.put("username", rs.getString("username"));
        botInfo.put("register_ip", rs.getString("register_ip"));
        myMap.add(c, botInfo);
        c++;
    }

The idea is to have the output be like this.. and I'll use PHP as an example:

[0] => array('username' => 'someUsername', 'register_ip' => 'someIP');

And so on.. but, what I get is a list of 10 HashMaps that all contain the same username,register_ip value. What are my options?

Steve
  • 2,936
  • 5
  • 27
  • 38
  • you should use `LinkedList` instead of `ArrayList`, this is why: http://stackoverflow.com/questions/5846183/arraylist-vs-linkedlist – yegor256 Sep 10 '12 at 06:31

4 Answers4

4

That is because you're adding the same botInfo HashMap to your List. What you would want to do is create a new HashMap inside your while loop, add elements to it and finally add this map to your list. What you would want to do is something like this:

ResultSet rs = DB.doQuery("SELECT username,register_ip FROM users ORDER BY joined DESC LIMIT 10");
List<Map<String , String>> myMap  = new ArrayList<Map<String,String>>();
int c = 0;

while(rs.next()) {
    //Create a new instance of the map
    Map<String,String> botInfo = new HashMap<String, String>();

    //Add elements to the the map
    botInfo.put("username", rs.getString("username"));
    botInfo.put("register_ip", rs.getString("register_ip"));

    //Add the map to the list
    myMap.add(c, botInfo);

    //Increment your counter
    c++;
}
Sujay
  • 6,753
  • 2
  • 30
  • 49
2
ResultSet rs = DB.doQuery("SELECT username,register_ip FROM users ORDER BY joined DESC LIMIT 10");
List<Map<String , String>> myMap  = new ArrayList<Map<String,String>>();

while(rs.next()) {
    Map<String,String> botInfo = new HashMap<String, String>();
    botInfo.put("username", rs.getString("username"));
    botInfo.put("register_ip", rs.getString("register_ip"));
    myMap.add(botInfo);
}

You need a new HashMap for botInfo for every record.

EDIT: c was useless

1
ResultSet rs = DB.doQuery("SELECT username,register_ip FROM users ORDER BY joined DESC LIMIT 10");
Map<String,String> botInfo;
List<Map<String , String>> myMap  = new ArrayList<Map<String,String>>();

while(rs.next()) {
    botInfo = new HashMap<String, String>();
    botInfo.put("username", rs.getString("username"));
    botInfo.put("register_ip", rs.getString("register_ip"));
    myMap.add(botInfo);
}

Use this code.. This will help you.

Bathakarai
  • 1,517
  • 6
  • 23
  • 39
0

Try this one:

ResultSet rs = DB.doQuery("SELECT username,register_ip FROM users ORDER BY joined DESC LIMIT 10");
Map<String,String> botInfo;
List<Map<String , String>> myMap  = new ArrayList<Map<String,String>>();
String[] columns = {"username", "register_ip"};

while(rs.next()) {
    botInfo = new HashMap<String, String>();
    for(int i = 0; i < columns.length; i++) {
        botInfo.put(columns[i], rs.getString(columns[i]));
    }
    myMap.add(botInfo);
}
Crazenezz
  • 3,416
  • 6
  • 31
  • 59