When to use add and when to use put in ArrayLists/HashMaps?


In Java when do you use put when adding data and when do you use add when adding data. Do you use add only for ArrayLists and put for HashMaps or can you use them both for ArrayLists and Hashmaps?

Replace this line with your code.


You'd look up which operations the data structure offers and use that.

You can't "use both" because applyng an ArrayList's method on a HashMap wouldn't make any sense - they are different data structures, operations on them are different. They certainly may have methods by the same name, they will likely still do something different as the same word can mean different things in different contexts. Opening a can and opening a bag for example, are different actions. The method of opening a bag will not apply to a can.



This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.