HashMap in Java

HashMap class implements the Map interface in Java. Some important points about HashMap class-

  • HashMap store the elements in Key-Value pair.
  • HashMap contains only unique elements i.e You can’t use the same key-value pair again.
  • HashMap may have one null key and multiple null values.
  • HashMap maintains no order.
  • HashMap is non-synchronized class.

 

Example of HashMap

Let’s understand the HashMap with this easy example.

Output:

key : 1 Value : Eden
key : 2 Value : Anshul
key : 3 Value : Rubi
key : 4 Value : Matt
key : 5 Value : Mirilla
Value at index 2 is: Anshul

 

In the above example first, we have implemented an HashSet then Inserted the values in the HashMap and Print the values.

Like this, we can implement the HashMap in Java language.

 

Methods of HashMap Class

HashMap class has a lot of methods –

  • void clear() : It removes all the key and value pairs from the specified Map.
  • boolean containsKey(Object key) : It is a boolean function which returns true or false based on whether the specified key is found on the map.
  • boolean containsValue(Object Value) : Similar to containsKey() method, however it looks for the specified value instead of key.
  • Value get(Object key) : It returns the value for the specified key.
  • boolean isEmpty() : It checks whether the map is empty.
  • value put(Key k, Value v) : Inserts key value mapping into the map. Used in the above example.
  • int size() : Returns the size of the map – Number of key-value mappings.

 

Selenium Tutorial:

If you liked this video, then please subscribe to our YouTube Channel for more video tutorials.

0 Comment

Leave a Reply