Map Interface in Java

In Java, various types of data structures are available for different purposes. Among these, the Map interface is a fundamental data structure that provides options to store and manipulate key-value pairs.

Map Interface in java

Map Interface Properties

  • Map is not the child interface of Collection.
  • If we want to represent a group of objects as key-value pair then we should go for Map.
  • Duplicate Keys are not allowed but duplicate values are allowed.
  • Each key-value pair is known as an entry, Hence map is considered a collection of entry objects.

Implementation of Map

In Java, the following implementation of the Map interface is available.

  • HashMap
  • LinkedHashMap
  • TreeMap

Map Interface Method

put(Object key, Object value)

public Object put(Object key, Object value) 

This method is used to add a key-value pair to the Map, if the key is already present then a new value will be updated for that key, and the old value will be returned. It returns null if the key is not already present.

putAll(Map m)

public void putAll(Map m)

get(Object key)

public Object get(Object key)

this method returns the value associated with the provided key

remove(Object key)

public Object get(Object key)

this method removes the entry (Key – value) associated with the provided key

isEmpty()

public boolean isEmpty()

This method returns true if the map contains no key-value mappings

containsKey(Object key)

public boolean containsKey(Object key) 

This method Returns true if this map contains a mapping for the specified key else it returns false.

Similar Java Tutorials

Leave a Comment