How to add elements in HashMap (Key/Value)

To add elements in HashMap we use V Map.put(K key, V value) method. Elements in Hashmap are added in a Key/Value pair.

Points to Remember about put() method

1. put() method maps the given key to the value. If a key already exits in the HashMap, then the old value is replaced with new value.

2. put() method returns the previously associated value for that Key. If the HashMap does not contain that key, then put() method returns a null value.

Example : Add Elements in HashMap



1. HashMap