Values ​​are overridden and not added

The values ​​in the list are overridden in my program. I want to use the same object to add different values.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Scanner;

public class CommonValue {
    static int key = 100;

    public static void main(String[] args) throws IOException {
        HashMap<Integer, ArrayList<String>> map = new HashMap<Integer, ArrayList<String>>();
        ArrayList<String> list = new ArrayList<String>();
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        StringBuffer sBuffer = new StringBuffer();
        Scanner scan = new Scanner(System.in);
        String choice = null;
        do {
            System.out.println("enter the how many element to add");
            int numOfElement = Integer.parseInt(reader.readLine());
            String userInput;
            int i = 0;
            do {
                // adding element in the list
                System.out.println("enter the element to add in the list");
                userInput = scan.next();
                list.add(userInput);
                i++;
            } while (i < numOfElement);
            // adding list in the map with key
            map.put(key, list);
            System.out.println(map);
            list.clear();
            // my intial key is 100 and it will incremented when i am going for another key
            key++;
            System.out.println("do you want to go for next key");
            System.out.println("y or n");
            choice = scan.next();

        } while (choice.equals("y"));
        for (Entry<Integer, ArrayList<String>> entry : map.entrySet()) {
            key = entry.getKey();
            ArrayList<String> value = entry.getValue();
            System.out.println("key" + entry.getKey() + ": value " + entry.getValue());
        }
    }
} 

      

Output:

enter the number of items to add 2
enter the item to add to the list

enter the item to add to the list
x
{100 = [a, x]}
you want to move to the next key
y or n
y
enter the number of items to add 1
enter the item to add to the list
z
{100 = [z], 101 = [z]}
you want to go to the next key
y or n

Actually, I want the output:

{100 = [a, x], 101 = [z]}

+3


source to share


3 answers


The problem is you keep adding the same instance List

to Map

without creating a copy. This won't work as clearing the list outside the map also clears the list inside the map - it's the same object after all.



Replace list.clear();

with list = new ArrayList<String>();

to fix this problem.

+6


source


You need to create a new one List

for each entry in the HashMap

. You are currently adding the same List instance to every record. Combined with list.clear()

this results in the observed output. The last entries in the list (the only one!) Will determine the output for each key.



+2


source


Dear you are wrong on the belove line

list.clear();

      

instead, just re-initialize the list with a new instance like

list = new ArrayList<String>();

      

+2


source







All Articles