HashMap in Java

What is HashMap in Java ?

HashMap in Java is a hash table based implementation of Map Interface. It is used to store key-value pairs. HashMap is a widely used collection classes in java.  To access a value in HashMap, you need to have a key.

HashMap extends AbstractMap and implements Serializable, Cloneable and Map interfaces. HashMap class declaration is as follows:

public class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneable, Serializable

HashMap class is simmilar to Hashtable, except that it is not synchronized and it allows null key and values. If multiple threads access a hash map concurrently, and at least one of the threads modifies the map structurally, it must be synchronized externally. This is typically accomplished by synchronizing on some object that naturally encapsulates the map. If no such object exists, the map should be “wrapped” using the Collections.synchronizedMap method. This is best done at creation time, to prevent accidental unsynchronized access to the map:

HashMap in Java is an unordered collection which means that this class does not guarantee that the order will remain constant over the time.

Important Points about HashMap:

  • HashMap allows null key and null values.
  • HashMap can have one null key and multiple null values.
  • HashMap is not an ordered collection.
  • Java HashMap is not thread safe, for multithreaded environment you should use ConcurrentHashMap class or get synchronized map using Collections.synchronizedMap() method.

 

Methods provided by HashMap class:

HashMap class provides following methods.

  • void clear(): This method removes all the key and value pairs from the Map.
  • Object clone(): This method returns a shallow copy of all the HashMap interface.
  • boolean containsKey(Object key): This method returns true if the map contains a mapping for the specified key
  • boolean containsValue(Object Value): This method is similar to containsKey() method, however it looks for the specified value instead of key.
  • value put(Key k, Value v): Inserts key value mapping into the map.
  • int size(): This method returng the number of key-value mappings in the map.
  • Collection values(): This method returns a collection of values contained in the map.
  • Value get(Object key): This method returns the value for the specified key. If this map contains no mapping for the key, then it returns null.
  • boolean isEmpty(): This method checks whether the map is empty or not. If there are no key-value mapping present in the map then this function returns true.
  • Value remove(Object key): This method removes the key-value pair for the specified key if the key is present.
  • void putAll(Map map): This method copies all the elements of a map to the another specified map.
  • keySet(): This method returns the Set of the keys contained in the map.

 

HashMap example:

Check the output:

How to remove a value from HashMap using key ?

Let’s see how to remove value from HashMap using key with an example.

Output:

Related posts

Leave a Comment