Map Interface In Java

Java Map stores elements in the form of Key-Value pair where it maps keys to values.

A map cannot contain duplicate keys and each key can map to at most one value where each pair of key-value is known as an Entry. If you want to perform operations based on key-value pair then Java Map is very useful.

Map Interface in Java is not part of Java Collection because its working is a bit different.

The map is an interface so you need to use its implementation class in order to use it. In my knowledge, Hashmap and Treemap are the most commonly used, implementation classes.

Some more implementations are as follow:

  • java.util.HashMap
  • java.util.LinkedHashMap
  • java.util.Hashtable
  • java.util.IdentityHashMap
  • java.util.WeakHashMap
  • java.util.EnumMap
  • java.util.TreeMap
  • java.util.Properties

Each of the above implementations has some unique features and can be used for different purposes.

For Example,

  • In Hashmap, It may have multiple null values but the insertion order is not preserved.
  • In LinkedHashMap, insertion order is preserved, also fast in performance.
  • In TreeMap, It contains unique elements and doesn’t allow null keys & values. Also, elements are sorted in a natural manner.

Method of Map Interface:

Below are some commonly used methods of map interface in Java.

Method NameDescription
int size()This method returns the number of entries in the map.
V put(Object key, Object value)It is used to insert an entry in the map.
void putAll(Map map)It is used to insert the specified map in the map.
V putIfAbsent(K key, V value)It inserts the element with key-value pair if not already exists.
V remove(Object key)It is used to delete an entry for the specified key.
boolean remove(Object key, Object value)It removes the specified values with the associated keys from the map.
Set keySet()It returns the Set view containing all the keys.
Set<Map.Entry<K,V>> entrySet()It returns the Set view containing all the keys and values.
void clear()It is used to reset the map.
boolean containsValue(Object value)This method returns true if some value equal to the value exists within the map, else return false.
boolean containsKey(Object key)This method returns true if some key equal to the key exists within the map, else return false.
boolean equals(Object o)It is used to compare the specified Object with the Map.
void forEach(BiConsumer<? super K,? super V> action)It iterates all over the map until all entries have been processed or entries throw an exception.
V get(Object key)This method returns the object that contains the value associated with the key.
V getOrDefault(Object key, V defaultValue)It returns the value to which the specified key is mapped, or defaultValue if the map contains no mapping for the key.
int hashCode()It returns the hash code value for the Map
boolean isEmpty()This method returns true if the map is empty; returns false if it contains at least one key.
V replace(K key, V value)It replaces the specified value for a specified key.
boolean replace(K key, V oldValue, V newValue)It replaces the old value with the new value for a specified key.
void replaceAll(BiFunction<? super K,? super V,? extends V> function)It replaces each entry’s value.
Collection values()It returns a collection view of the values contained in the map.

Java Map Examples:

Let’s see some of the examples of above given method:

1. How to create a Map:

    Map<String, String> map1 = new HashMap<>();

    Map<Integer, String> map2 = new LinkedHashMap<>();

    Map<String, List<String>> map3 = new TreeMap<>();

    Map<String, Map<String, String>> map4 = new WeakHashMap<>();

2. How to add and remove elements from Map:

  public static void main(String[] args) {

    Map<Integer, String> map = new HashMap<>();

    map.put(1, "John");
    map.put(2, "Harry");
    map.put(3, "Robert");
    map.put(4, "David");

    System.out.println("Printing map :: " + map);

    // removing element with key 2

    System.out.println("Printing map after removal:: " + map);



Printing map :: {1=John, 2=Harry, 3=Robert, 4=David}
Printing map after removal:: {1=John, 3=Robert, 4=David}

3. How to iterate Map:

  public static void main(String[] args) {

    Map<Integer, String> map = new HashMap<>();

    map.put(1, "John");
    map.put(2, "Harry");
    map.put(3, "Robert");
    map.put(4, "David");

    // iterating using typical for loop
    for (Map.Entry<Integer, String> obj : map.entrySet()) {
      System.out.println(obj.getKey() + "=" + obj.getValue());

    // iterating over keys
    for (Integer obj : map.keySet()) {

    // iterating using Java 8 stream

    // iterating using forEach()
    map.forEach((key, value) -> System.out.println(key + " = " + value));


4. Inserting NULL keys and values:

  public static void main(String[] args) {

    Map<Integer, String> map = new HashMap<>();

    map.put(1, "John");
    map.put(2, null);
    map.put(null, "Robert");




5. Using putIfAbsent() and getOrDefault():

  public static void main(String[] args) {

    Map<Integer, String> map = new HashMap<>();

    map.put(1, "John");
    map.put(2, "Harry");
    map.put(3, "Robert");
    map.put(4, "David");

    // add value if not exists
    map.putIfAbsent(4, "Michael");
    map.putIfAbsent(5, "Joe");


    // print value if exists else print Not Found
    System.out.println(map.getOrDefault(4, "Not Found"));
    System.out.println(map.getOrDefault(10, "Not Found"));



{1=John, 2=Harry, 3=Robert, 4=David, 5=Joe}
Not Found

That’s it for this post. Hope you’ve understand Map Interface in Java and some of its methods.

Happy Learning!