Steve
Steve

Reputation: 3046

Add a hashmap to an array of hashmaps

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?

Upvotes: 0

Views: 3038

Answers (4)

Crazenezz
Crazenezz

Reputation: 3456

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

Upvotes: 0

Bathakarai
Bathakarai

Reputation: 1537

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.

Upvotes: 1

user180100
user180100

Reputation:

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

Upvotes: 2

Sujay
Sujay

Reputation: 6783

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

Upvotes: 4

Related Questions