Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




Java - Map

Map Interface is a part of collections framework. In a Map data is stored in the form of Key and Value pair.


Example :

You can relate it with a Bank locker. Where each locker has a key and we can keep something inside the locker. Just compare the locker key with the Map key and the stuff you keep inside the locker is the Map value.


Points on Map

  1. In a Map a key is needed to access the values and the key should be unique.

  2. A Key and a Value are a pair and each key - value pair is termed as 'Entry'.

  3. A Map has no relation with the Collection Interface.

  4. A Map allows duplicate values but does not allow duplicate keys.


Below is the Hierarchy for Map :




Methods used in Map :

  1. public Object put(Object key, Object value)

  2. put(..) method is used to Insert a value to the Map.

    It is just like creating a new locker in a bank and you get the key of the locker after that


  3. public Object get(Object key)

  4. get(..) method is used to get the value from the Map.

    It is just like getting your valuables from the bank locker. You already have a key for your bank locker. Now, you need to go to your locker and use the key to get your valuables from it.


  5. public Object remove(Object key)

  6. remove(..) method is used to delete an existing value from the Map using the key.

    It is just like closing your Bank locker. You go to your locker, open it with the key, get your valuables and close it permanently.


  7. public Set keySet()

  8. keySet() method is used to get all the keys associated to a Map. And if you see the return type, it is of type Set.

    It is just like, you being a bank manager, want to get the keys of all locker holders.


  9. public Set entrySet()

  10. entrySet() method is used to get all the keys and values associated to a Map. Even in this case the method returns a Set.

    In other words, entrySet() returns all the Entries associated to a Map.

    Just for the sake of understanding, just consider it to be like, you being a Bank Manager, want to get all the Keys and also the contents of all the lockers.


  11. boolean containsKey(Object key)

  12. containsKey() method is used to check, if a key is present in the Map or not.


  13. boolean containsValue(Object value)

  14. containsValue() method is used to check, if a value is present in the Map or not.


  15. boolean isEmpty()

  16. isEmpty() method is used to check, if a Map is Empty or not.


  17. int size()

  18. size() method tells us the number of Entries present in the Map.


The Entry Interface

A Key - Value pair in a Map is termed as an Entry. Since, a Map is a group of Key - Value pair, the Entry Interface is defined inside the Map Interface. i.e. An Entry interface cannot exists on its own.


Methods in Entry Interface :

  1. Object getKey()

  2. It is used to get the key of a Map Entry.


  3. Object getValue()

  4. It is used to get the value of a Map Entry.


  5. Object setValue()

  6. It is used to replace the Value of a Map Entry.