Map Interface in Java

Map Interface

In the previous blog, we learned about ArrayDeque. If you want to know more about it, visit ArrayDeque in Java. In this blog, we will go through the Map Interface.


Map Interface

Map interface maps the keys with values meaning elements in the Map are stored in key-value pairs. Each key is a unique key associated with individual values. Map cannot contain duplicate keys. Map interface is present in java.util package. A Map is useful when we want to search, update or delete an element on the basis of the key. The examples where Map can be used are:

  1. A map roll no and students
  2. A map of zip codes and cities, etc

Syntax of Map

Map map = new HashMap();

Classes that implements Map interface

...

Classes that implements Map Interface are shown above

  1. TreeMap
  2. HashMap
  3. LinkedListMap
  4. EnumMap
  5. WeakHashMap

Methods of Map

  1. put(Object k, Object v)
  2. put(Object k, Object v) method is used to insert or add an entry to the Map.

    e.g.

    import java.util.*;
    class MapPutEx {
      public static void main(String[] args) {
        Map<Integer, String> map = new HashMap<>();
        map.put(101, "John");
        map.put(102, "Smith");
        map.put(103, "Chris");
        map.put(104, "Donna");
        System.out.println("Map: "+ map);
      }
    }

    Output

    Map: {101=John, 102=Smith, 103=Chris, 104=Donna}
  3. size()
  4. size() method is used to get the size of the map which refers to the number of key and value pairs.

    e.g.

    import java.util.*;
    class MapSizeEx {
      public static void main(String[] args) {
        Map<Integer, String> map = new HashMap<>();
        map.put(101, "John");
        map.put(102, "Smith");
        map.put(103, "Chris");
        map.put(104, "Donna");
        System.out.println("Map: "+ map);
        System.out.println("Size of Map: "+ map.size());
      }
    }

    Output

    Map: {101=John, 102=Smith, 103=Chris, 104=Donna}
    Size of Map: 4
  5. putAll()
  6. The putAll() method inserts or adds the specified map to the map.

    e.g.

    import java.util.*;
    class MapPutAllEx {
      public static void main(String[] args) {
        Map<Integer, String> map = new HashMap<>();
        map.put(101, "John");
        map.put(102, "Smith");
        map.put(103, "Chris");
        map.put(104, "Donna");
        System.out.println("Map1: "+ map);
        Map<Integer, String> map1 = new HashMap<>();
        map1.put(105, "Charles");
        map1.put(106, "Smith");
        map1.put(107, "Bren");
        map1.put(108, "Bruce");
        System.out.println("Map2: "+ map1);
        map.putAll(map1);
        System.out.println("Output of the combined elements of Map1 and Map2 using putAll():"+ map);
      }
    }

    Output

    Map1: {101=John, 102=Smith, 103=Chris, 104=Donna}
    Map2: {105=Charles, 106=Smith, 107=Bren, 108=Bruce}
    Output of the combined elements of Map1 and Map2 using putAll():{101=John, 102=Smith, 103=Chris, 104=Donna, 105=Charles, 106=Smith, 107=Bren, 108=Bruce}
  7. clear()
  8. clear() method is used to clear or remove all the elements from the specified Map.

    e.g.

    import java.util.*;
    class MapClearEx {
      public static void main(String[] args) {
        Map<Integer, String> map = new HashMap<>();
        map.put(101, "John");
        map.put(102, "Smith");
        map.put(103, "Chris");
        map.put(104, "Donna");
        System.out.println("Map1: "+ map);
        Map<Integer, String> map1 = new HashMap<>();
        map1.put(105, "Charles");
        map1.put(106, "Smith");
        map1.put(107, "Bren");
        map1.put(108, "Bruce");
        System.out.println("Map2: "+ map1);
        map.clear();
        System.out.println("Displayed output after using clear() method on Map: "+ map);
      }
    }

    Output

    Map1: {101=John, 102=Smith, 103=Chris, 104=Donna}
    Map2: {105=Charles, 106=Smith, 107=Bren, 108=Bruce}
    Displayed output after using clear() method on Map: {}
  9. get(Object k)
  10. get(Object k) method returns the object that contains the value associated with the key.

    e.g.

    import java.util.*;
    class MapGetEx {
      public static void main(String[] args) {
        Map<Integer, String> map = new HashMap<>();
        map.put(101, "John");
        map.put(102, "Smith");
        map.put(103, "Chris");
        map.put(104, "Donna");
        System.out.println("Map1: "+ map);
        Map<Integer, String> map1 = new HashMap<>();
        map1.put(105, "Charles");
        map1.put(106, "Smith");
        map1.put(107, "Bren");
        map1.put(108, "Bruce");
        System.out.println("Map2: "+ map1);
        System.out.println("Displaying the value of element : "+ map.get(103));
        System.out.println("Displaying the value of element : "+ map1.get(105));
      }
    }

    Output

    Map1: {101=John, 102=Smith, 103=Chris, 104=Donna}
    Map2: {105=Charles, 106=Smith, 107=Bren, 108=Bruce}
    Displaying the value of element : Chris
    Displaying the value of element : Charles
  11. getOrDefault(Object k, V defaultValue)
  12. getOrDefault(Object k, V defaultValue) method returns the value to which the specified key is mapped, or if no value is found then it returns defaultValue.

    e.g.

    import java.util.*;
    class MapGetOrDefaultEx {
      public static void main(String[] args) {
        Map<String, Integer> map = new HashMap<>();
        map.put("John", 101);
        map.put("Smith", 102);
        map.put("Chris", 103);
        map.put("Donna", 104);
        System.out.println("Map1: "+ map);
        int key = map.getOrDefault("Donna", 105);
        System.out.println("The value of element is: "+ key);
      }
    }

    Output

    Map1: {Donna=104, Chris=103, Smith=102, John=101}
    The value of element is: 104
  13. containsKey(Object k)
  14. containsKey(Object k) method is used to verify or check if the specified key k is present in the map or not.

    e.g.

    import java.util.*;
    class MapContainsKeyEx {
      public static void main(String[] args) {
        Map<Integer, String> map = new HashMap<>();
        map.put(101, "John");
        map.put(102, "Smith");
        map.put(103, "Chris");
        map.put(104, "Donna");
        System.out.println("Map1: "+ map);
        Map<Integer, String> map1 = new HashMap<>();
        map1.put(105, "Charles");
        map1.put(106, "Smith");
        map1.put(107, "Bren");
        map1.put(108, "Bruce");
        System.out.println("Map2: "+ map1);
        System.out.println("Is the specified element present?: "+ map.containsKey(103));
        System.out.println("Is the specified element present?: "+ map1.containsKey(108));
        System.out.println("Is the specified element present?: "+ map1.containsKey(109));
      }
    }

    Output

    Map1: {101=John, 102=Smith, 103=Chris, 104=Donna}
    Map2: {105=Charles, 106=Smith, 107=Bren, 108=Bruce}
    Is the specified element present?: true
    Is the specified element present?: true
    Is the specified element present?: false
  15. containsValue(Object v)
  16. containsValue(Object v) method is used to verify or check if the specified value v is present in the map or not.

    e.g.

    import java.util.*;
    class MapContainsValueEx {
      public static void main(String[] args) {
        Map<Integer, String> map = new HashMap<>();
        map.put(101, "John");
        map.put(102, "Smith");
        map.put(103, "Chris");
        map.put(104, "Donna");
        System.out.println("Map1: "+ map);
        Map<Integer, String> map1 = new HashMap<>();
        map1.put(105, "Charles");
        map1.put(106, "Smith");
        map1.put(107, "Bren");
        map1.put(108, "Bruce");
        System.out.println("Map2: "+ map1);
        System.out.println("Is the specified element present?: "+ map.containsValue("Donna"));
        System.out.println("Is the specified element present?: "+ map1.containsValue("Charles"));
        System.out.println("Is the specified element present?: "+ map1.containsValue("Joe"));
      }
    }

    Output

    Map1: {101=John, 102=Smith, 103=Chris, 104=Donna}
    Map2: {105=Charles, 106=Smith, 107=Bren, 108=Bruce}
    Is the specified element present?: true
    Is the specified element present?: true
    Is the specified element present?: false
  17. remove(Object k)
  18. remove(Object k) method removes the entry from the map represented by the key k.

    e.g.

    import java.util.*;
    class MapRemoveEx {
      public static void main(String[] args) {
        Map<Integer, String> map = new HashMap<>();
        map.put(101, "John");
        map.put(102, "Smith");
        map.put(103, "Chris");
        map.put(104, "Donna");
        System.out.println("Map: "+ map);
        map.remove(102);
        System.out.println("After removing the element the updated the map is:" + map);
        Map<Integer, String> map1 = new HashMap<>();
        map1.put(105, "Charles");
        map1.put(106, "Smith");
        map1.put(107, "Bren");
        map1.put(108, "Bruce");
        System.out.println("Map1: "+ map1);
        map1.remove(107);
        map1.remove(109);
        System.out.println("After removing the element the updated the map1 is: " + map1 );
      }
    }

    Output

    Map: {101=John, 102=Smith, 103=Chris, 104=Donna}
    After removing the element the updated the map is:{101=John, 103=Chris, 104=Donna}
    Map1: {105=Charles, 106=Smith, 107=Bren, 108=Bruce}
    After removing the element the updated the map1 is: {105=Charles, 106=Smith, 108=Bruce}

Core Java Tutorial

Get in Touch

Atrowel will be pleased to receive your feedback and suggestions. Those of you who would like to contribute, please send us an email.