Karla News

How to Use HashMap Class in Java

Mostly HashMap is used when we have very less data and we don’t want to store them into the database or into any other types of data file. Advantage of storing data into HashMap is, easy and fast access of stored data. While accessing data from HashMap we don’t have to perform even single iteration on all data but we just have to specify ‘key’ which is associated with ‘Value’ which we want to retrive. We can also increase performance of our application in some extend using HashMap.

In HashMap we store data in the form of ‘key’ and ‘value’ pair.

Hash table based implementation is on Map interface which provides all optional map operations and also permit ‘null’ key and ‘null’ value. HashMap is also equivalent to Hashtable except in HashMap we cannot store null key / value.

I have also observed that HashMap never maintains order of stored data. If we will store same sets of data each time into HashMap then also order of key/value pair will be different each time.

One important thing about HashMap is; its implementation is not synchronized.

In HashMap class we have many methods available, let’s now discuss each important method how we can use different functions/methods available in HashMap class.


Constructors In HashMap Class

When we create any HashMap we can also specify initial capacity and load factor of the HashMap instead of using default values and that can be done with the help of different available constructors.

When we want to use default value for initial capacity (limit of 16) then we can use below constructor.

See also  The Ultimate Guide to Free (and Legal) or Cheap Music Downloads and Songs

HashMap ( )

{ It has initial default capacity of 16 and default load factor is 0.75 }

When we just want to use HashMap with our own initial capacity then we have to use constructor mentioned below.

HashMap ( int initialCapacity )

{ Default load factor is 0.75 with user defined initial Capacity }

In case if we want to select our own initial capacity and load factor then we have to use below constructor.

HashMap ( int initialCapacity, float loadFactor )

HashMap ( Map m ) is used when we want to construct a new HashMap with the same mappings as the specified form Map.

Example of how we can create HashMap with user defined InitialCapicity.

HashMap demoHashMap = new HashMap(5000);

Methods available in HashMap Class

We are not going to discuss all methods available in HashMap class but few of them which are very important and also used most of the time.

There are two methods which are mostly used in HashMap class.

put (Object key, Object value)

get (Object key)

If we want to insert any value into the created HashMap, we can use ‘put()’ method.

Syntax: demoHashMap.put ( key, value )

If we want to retrieve any value from the HashMap, we can do it with the help of ‘get()’ method.

Syntax: demoHashMap.get ( key ) which returns value associated with specified key in get() method.

We can also remove single HashMap entry or all entries. To remove single entry we can use remove ( Object key ) method. We have to pass ‘key’ to this function whose ‘Value’ we want to remove from HashMap. To Remove all mapping we can use clear() method.

See also  Digitally Obsessed: 6 Most Addictive PC and Web Games

demoHashMap.clear ( )

demoHashMap.remove ( key )

To know how many entries are available in HashMap, we can use size() method which returns integer value (number of entries in Map).

We can also perform cloning of one HashMap with the help of clone() method and if we want to put all entries from one Map to another then putAll(Map m) method can help us.

I hope after reading this article you will be able to use HashMap class methods very easily. Let me know if you are still have any doubts related to HashMap.

Thanks a lot!