Search code examples
javaadditionputdifference

Java List.add() and Map.put()


Working with Lists and Maps, I started thinking, why Map method for adding Object is called put and for List it is add?

It is just developers will, or there was something under naming those methods different.

May be the methods names let the developer know while adding to Map/List what kind of data structure he is working with? Or those names describes the way they works?


Solution

  • The difference is :

    1. .add() means to insert something at the end or wherever you want to(you know where to add) whereas
    2. .put() means to add an element wherever it needs to be placed, not necessarily at the end of the Map, because it all depends on the Key to be inserted (you don't know where to add).