How to use HashMap In Java

Google Sponsored Links

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

public static void main(String args[]){
HashMap<String, String> Test = new HashMap<String, String>();

//adding Key-Value Pair to HashMap
Test.put("first", "First value inserted");
Test.put("Second", "Second value inserted");
Test.put("Third", "Third value inserted");
//Getting value for the given Key from HashMap
System.out.println("Value of Second: "+Test.get("Second"));
System.out.println("Is HashMap Empty? "+Test.isEmpty());

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( )


mapHttpErrors.clear() {
    System.out.println("Is map empty? " + mapHttpErrors.isEmpty());
  • boolean containsKey(Object k)


if (mapHttpErrors.containsKey("200")) {
    System.out.println("Http status 200");
  • boolean containsValue(Object v)


Google Sponsored Links
if (mapHttpErrors.containsValue("OK")) {
    System.out.println("Found status OK");
  • boolean isEmpty( )


if (mapHttpErrors.isEmpty()) {
    System.out.println("No Error");
} else {
    System.out.println("Have HTTP Errors");
  • Set entrySet( )


Set<Map.Entry<String, String>> entries = mapCountryCodes.entrySet();
for (Map.Entry<String, String> entry : entries) {
    String code = entry.getKey();
    String country = entry.getValue();
  • Set keySet( )


Map<String, String> mapCountryCodes = new HashMap<>();
mapCountryCodes.put("1", "USA");
mapCountryCodes.put("44", "United Kingdom");
mapCountryCodes.put("33", "India");
mapCountryCodes.put("81", "China");
Set<String> setCodes = mapCountryCodes.keySet();
Iterator<String> iterator = setCodes.iterator();
while (iterator.hasNext()) {
    String code =;
    String country = mapCountryCodes.get(code);
    System.out.println(code + " => " + country);
  • int size( )


int size = mapHttpErrors.size();
  • Collection values( )


Collection<String> countries = mapCountryCodes.values();
for (String country : countries) {


For complete example of HashMap implementation visit git repo of Codedbug

Frequently Asked Questions On HashMap In Java

Yes, HashMap also allows null value, you can store as many null values as you want
1. Hashtable is synchronized whereas HashMap is not. 2. HashMap allows null values whereas Hashtable doesn’t allow null values.
It is a hashMap that allows concurrent modifications from multiple threads, as there can be multiple locks on the same hashmap.
If a key already exists in a HashMap and we try to add another value for the same key, then the old value of the key will be replaced with the new value.
Yes, for Hashtable. Hashtable implements Map interface.
Google Sponsored Links