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

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

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?

<Below this line, add a link to the EXACT exercise that you are stuck at.>

<In what way does your code behave incorrectly? Include ALL error messages.>

```

Replace this line with your code.

<do not remove the three backticks above>

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.

https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html
https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html

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