Map interface in java

Map interface in java

In Java, the Map interface is part of the Java Collections Framework and represents a collection of key-value pairs. Each key is associated with exactly one value, and keys are unique within the Map. The Map interface does not extend the Collection interface like most other collection types in Java.

Map interface in java

Features of the Map Interface:

  • 1. Key-Value Pairing: The Map interface stores elements as key-value pairs, where each key is associated with exactly one value. Keys are used to retrieve their corresponding values.
  • 2. No Duplicate Keys: Each key in a Map must be unique. If an attempt is made to add a duplicate key, the new value will overwrite the existing value associated with that key.
  • 3. Key-Based Lookup: Maps provide fast lookup operations based on keys. You can retrieve the value associated with a key quickly using the get() method.
  • 4. Iterable: Although the Map interface itself does not extend the Iterable interface, it provides methods to iterate over its keys, values, or key-value pairs.
  • 5. Associative Storage: Maps are often used for associative storage, where values are accessed by their corresponding keys. This makes them suitable for various data manipulation tasks.

Common Methods of the Map Interface:

  • put(K key, V value): Associates the specified value with the specified key in the map.
  • get(Object key): Returns the value associated with the specified key, or null if the key is not present in the map.
  • containsKey(Object key): Returns true if the map contains a mapping for the specified key.
  • containsValue(Object value): Returns true if the map contains one or more keys mapped to the specified value.
  • remove(Object key): Removes the mapping for the specified key from the map, if present.
  • 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 mappings contained in the map.

Example Usage of the Map Interface
```java
import java.util.HashMap;
import java.util.Map;

public class MapExample {
    public static void main(String[] args) {
        // Create a Map (HashMap in this case)
        Map<String, Integer> map = new HashMap<>();

        // Add key-value pairs to the map
        map.put("Apple", 10);
        map.put("Banana", 20);
        map.put("Cherry", 15);

        // Get the value associated with a key
        int value = map.get("Banana");
        System.out.println("Value associated with 'Banana': " + value);

        // Check if a key is present in the map
        boolean containsKey = map.containsKey("Apple");
        System.out.println("Contains 'Apple'? " + containsKey);

        // Remove a key-value pair from the map
        map.remove("Cherry");

        // Print all key-value pairs in the map
        System.out.println("Key-Value Pairs:");
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }
    }
}
```

In this example:

  • We create a HashMap (a type of Map) and add some key-value pairs to it.
  • We retrieve the value associated with the key “Banana” and check if the key “Apple” is present in the map.
  • We remove the key-value pair associated with the key “Cherry” from the map.
  • Finally, we iterate over all key-value pairs in the map and print them out.

The Map interface and its implementations, such as HashMap and TreeMap, are widely used in Java for various data storage and manipulation tasks where key-value pairs are involved.