0

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!!..

3 Answers 3

1

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());
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your reponse. Its working fine. have created hasmap within forloop and added into arraylist.
0
        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());
            }
        }

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.