shiva
shiva

Reputation: 177

how to get hashmap content of arraylist in java?

I am using the following code to save hashmap content into arraylist.

HashMap jediSaber = new HashMap();  
ArrayList<HashMap> valuesList = new ArrayList();

for(int i = 0; i< 4;i++) {
    jediSaber.put("white","white_name"+i);  
    jediSaber.put("blue","blue_name"+i);   
    valuesList.add(i, jediSaber);           
    System.out.println("list ontent:"+i+":"+valuesList.get(i).values());
}

    `   

output is as follows:

              list content:0:[blue_name0, white_name0]
              list content:1:[blue_name1, white_name1]
              list content:2:[blue_name2, white_name2]
              list content:3:[blue_name3, white_name3]

When i try to display the content of arraylist in outside with the following code,

System.out.println("list content:");
for(int i = 0;i<valuesList.size();i++){ 
    System.out.println("list:"+i+":"+valuesList.get(i).values());
}

It is showing the following output,

             list content:0:[blue_name3, white_name3]
             list content:1:[blue_name3, white_name3]
             list content:2:[blue_name3, white_name3]
             list content:3:[blue_name3, white_name3]

My problem is i need to display the content of arraylist of hashmap.

I think something i missed in second part. Can anybody help me to solve this minor issue?

Thanks in advance!!..

Upvotes: 0

Views: 1927

Answers (3)

hmjd
hmjd

Reputation: 122011

This is adding the same HashMap each time to the ArrayList:

valuesList.add(i, jediSaber);

Create a new HashMap each time within the for and add it:

List<HashMap<String, String>> valuesList =
    new ArrayList<HashMap<String, String>>();

for (int i = 0; i < 4; i++)
{
    HashMap<String, String> m = new HashMap<String, String>();
    m.put("white", "white_name" + i);
    m.put("blue",  "blue_name"  + i);
    valuesList.add(m);
}

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

Upvotes: 1

David Grant
David Grant

Reputation: 14243

Try pulling jediSaber inside your for loop, like so:

for(int i = 0; i < 4; i++) {
    Map<String, String> jediSaber = new HashMap<String, String>();

You should also parameterize valuesList too:

List<Map<String, String>> valuesList = new ArrayList<Map<String, String>>();

P.S. There's no need to call add(i, jediSaber) with the index argument: valuesList.add(jediSaber) will have the same effect.

Upvotes: 0

Subhrajyoti Majumder
Subhrajyoti Majumder

Reputation: 41230

        List<Map> valuesList = new ArrayList();
        for (int i = 0; i < 4; i++) {
            Map<Object, Object> jediSaber = new HashMap<>();
            jediSaber.put("white", "white_name" + i);
            jediSaber.put("blue", "blue_name" + i);
            valuesList.add(jediSaber);
            Set<Entry<Object, Object>> entrySet = jediSaber.entrySet();
            for (Entry<Object, Object> entry : entrySet) {
                System.out.println(entry.getKey() + "-" + entry.getValue());
            }
        }

Upvotes: 0

Related Questions