0

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));
asked Jun 15, 2015 at 20:45
0

1 Answer 1

1

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
answered Jun 15, 2015 at 20:57

3 Comments

the put is for Hashtable which contains <string Arraylist<string>>
@aditirana Read about duck typing in Java, HashTable is a Map. HashTable -> All Implemented Interfaces: Serializable, Cloneable, Map<K,V>
but this can work when the key is already present.I have to check data.containsKey(sub).For that case it is right but for the first entry of a key I have to add empty Arraylist.But I am unable to do so

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.