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

CONSTRUCTORDESCRIPTION
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

An example representing the implementation of HashMap In Java

Methods of HashMAp In Java Class

MethodDescription
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.

Use cases of HashMap and HashTable

There is no important difference between hash tables and hash maps 99% of the time. So, whatever we talk about one it’s true for the other.

Because of Key and Value pair, the use cases are numerous. Think of any situation where you want a quick addition, deletion, updating of data you can use HashMap. As you have the key in HashMap these tasks take less time as compared with any other collection. Or, if we have a huge volume of data that needs to be stored for processing then also using HashMap make sense. For example, let’s say we have 45,000 students in a university and some process needs to hold on to records for all of them. If you routinely refer to the student by ID number, then a ID => student cache makes excellent sense. The operation you are optimizing for this cache is fast lookup.

When not to use HashMap:

  • Iterate over the elements – HashMap typically does not do iteration very well. (Generic ones, that is. Particular implementations sometimes contain linked lists which are used to make iterating over them suck less. For example, in Java, LinkedHashMap lets you iterate over keys or values quickly.)
  • Sorting – If you can’t iterate, sorting is a pain
  • Going from value to key

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

void clear( )

Example:

boolean containsKey(Object k)

Example:

boolean containsValue(Object v)

Example:

boolean isEmpty( )

Example:

Set entrySet( )

Example:

Set keySet( )

Example:

int size( )

Example:

Collection values( )

Example:

Leave a Reply

Contact Us
close slider
Contact Us

How can we help you with?