Insert key value to HashTable when value is an ArrayList

982 Views Asked by At

I am running a for loop over some string values and I want to update the Arraylist value (for a particular key) in the pass of the loop only but when I search how to put arraylist values for a key,I can find the solution where the value is updated after all the values of the arraylist is updated. Suppose for key sub I want to enter the string as soon as i get value val

                data.put(sub,new ArrayList<String>(val));
1

There are 1 best solutions below

3
On BEST ANSWER

Is there any way to add elements to the Arraylist (value) one by one rather than one time with put.

One can't add anything to the ArrayList by put(...), but by add()

If you want to add value one by one to the ArrayList you may use foreach loop:

for(TypeOfTheSingleElementFromTheSet variable: setOfElementsWhichImplementsIterable){
    list.add(variable);
}

After rephrasing the original post

data.get(sub).add(val); //where data is reference to an object which implements Map interface