How to use HashMap In Java

HashMap In Java is a Map based collection class that is used for storing Key-value pairs and is denoted as Hash Map<Key, Value> or Hash Map<K, V>. HashMap class implements the map interface by using a hash table. Hashing is the transformation of a string of characters into a usually shorter fixed-length value or key that represents the original string.

Hashing is used to index and retrieve items in a database, as it is faster to find the item using the shorter hashed key than to find it using the original value.

It stores only object references, that’s why it is impossible to use primitive data types like double or int. So, instead of using HashMap, we use wrapper classes (like Integer or Double) in java.HashMap In Java

Constructors of Java HashMap class

HashMap() HashMap is used to construct a default HashMap.
HashMap(Map m) HashMap is used to initializes the hash map by using the elements of the given Map object m.
HashMap(int capacity) HashMap is used to initializes the capacity of the hash map to the given integer value, capacity.
HashMap(int capacity, float fillRatio) HashMap is used to initialize both the capacity and fill ratio of the hash map by using its arguments.

 HashMap In Java Class

  • Implements Map and extends Abstract Map
  • Provides unsorted and un-ordered Map
  • HashMap allows one null key and numerous null values
  • HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls

Example representing implementation of HashMap In Java

Methods of HashMAp In Java Class

Method Description
void clear() HashMap is used to remove all of the mappings from this map.
boolean containsKey(Object key) HashMap is used to return true if this map contains a mapping for the specified key.
boolean containsValue(Object value) HashMap is used to return true if this map maps one or more keys to the specified value.
boolean isEmpty() HashMap is used to return true if this map contains no key-value mappings.
Object clone() HashMap is used to return a shallow copy of this instance: the keys and values themselves are not cloned.
Set entrySet() HashMap is used to return a collection view of the mappings contained in this map.
Set keySet() HashMap is used to return a set view of the keys contained in this map.
Object put(Object key, Object value) HashMap is used to associate the specified value with the specified key in this map.
int size() HashMap is used to return the number of key-value mappings in this map.
Collection values() HashMap is used to return a collection view of the values contained in this map.

Now let’s take a quick glimpse of implementation of the methods mentioned above :

  • void clear( )


  • boolean containsKey(Object k)


  • boolean containsValue(Object v)


  • boolean isEmpty( )


  • Set entrySet( )


  • Set keySet( )


  • int size( )


  • Collection values( )


For complete example of HashMap implementation visit git repo of Codedbug

Leave a Reply

Contact Us
close slider
Contact Us

How can we help you with?