Map in Java Collections | Explain map in Collections Framework?

 



The Map interface in Java is a part of the Java Collections Framework and provides a collection of key-value pairs. 

It provides several methods to perform operations on the key-value pairs, such as adding a new pair, retrieving the value for a specified key, removing a key-value pair, and more.


Implemented Classes:


HashMap: HashMap uses a hash table to store the key-value pairs. It provides constant time complexity for basic operations such as get and put. It does not maintain any order of the key-value pairs.


TreeMap: TreeMap uses a Red-Black tree data structure to store the key-value pairs. It provides logarithmic time complexity for the basic operations. TreeMap sorts the key-value pairs in ascending order of their keys.


LinkedHashMap: LinkedHashMap extends HashMap and maintains the order of the key-value pairs in which they were inserted. It provides constant time complexity for the basic operations.


ConcurrentHashMap: ConcurrentHashMap is a thread-safe implementation of the Map interface. It provides a high level of concurrency and scalability.


The Map interface and its implementations are widely used in Java applications to store and retrieve data. The choice of implementation depends on the specific requirements of the application, such as the time complexity for basic operations, the order of the key-value pairs, and the level of concurrency required.


The Map interface in Java provides a collection of key-value pairs and is part of the Java Collections Framework. 


It provides several methods to perform operations on the key-value pairs such as:


put(K key, V value): Adds the specified key-value pair to the map or replaces the existing value for the specified key.

get(Object key): Returns the value associated with the specified key or returns null if the key is not found in the map.

remove(Object key): Removes the key-value pair associated with the specified key from the map.

size(): Returns the number of key-value pairs in the map.

isEmpty(): Returns true if the map is empty, false otherwise.

containsKey(Object key): Returns true if the map contains the specified key, false otherwise.

containsValue(Object value): Returns true if the map contains the specified value, false otherwise.

keySet(): Returns a Set view of the keys contained in the map.

values(): Returns a Collection view of the values contained in the map.

entrySet(): Returns a Set view of the key-value pairs contained in the map.



Example of Map Interface:


import java.util.HashMap;
import java.util.Map;
 
public class MapExample {
   public static void main(String[] args) {
      // Create a map
      Map<String, Integer> map = new HashMap<>();
 
      // Add key-value pairs to the map
      map.put("John", 25);
      map.put("Jane", 30);
      map.put("Jim", 35);
 
      // Retrieve the value for a key
      System.out.println("John's age: " + map.get("John"));
 
      // Remove a key-value pair
      map.remove("Jane");
 
      // Check if a key is present in the map
      System.out.println("Is Jim present in the map? " + map.containsKey("Jim"));
 
      // Iterate through the map
      for (Map.Entry<String, Integer> entry : map.entrySet()) {
         System.out.println(entry.getKey() + ": " + entry.getValue());
      }
   }
}
 

The output of the above code will be:

John's age: 25
Is Jim present in the map? true
Jim: 35
John: 25


Post a Comment

Post a Comment (0)

Previous Post Next Post