Map Interface

Map Interface

A map is an object that stores associations between keys and values, or key/value pairs. Followings are the feature of Map..

  1. Both keys and values are objects.
  2. For a given key, we can findĀ corresponding value in the map.
  3. Some map accept null as key and value.
  4. A ClassCastException is thrown when an object is incompatible with the elements in a map.

Since Map is an interface we need to instantiate a concrete implementation of the interface in order to use it. following areĀ Map implementations in the Java Collections API:
  1. java.util.HashMap
  2. java.util.Hashtable
  3. java.util.LinkedHashMap
  4. java.util.TreeMap
  5. java.util.WeakHashMap
  6. java.util.IdentityHashMap
Commonly used methods of Map interface:
Object put(Object key, Object value)is used to insert an entry in this map.
void putAll(Map map)is used to insert the specified map in this map.
Object remove(Object key)is used to delete an entry for the specified key.
Object get(Object key)is used to return the value for the specified key.
boolean containsKey(Object key)is used to search the specified key from this map.
boolean containsValue(Object value)is used to search the specified value from this map.
public Set keySet()returns the Set view containing all the keys.
public Set entrySet()returns the Set view containing all the keys and values.

Leave a Reply

Your email address will not be published. Required fields are marked *