We will find out answers for following question

  • What is HashMap
  • Common methods available
HashMap
  • HashMap is a Map based collection class that is used to store Key & value pairs.
  • It is denoted as HashMap<Key, Value> or HashMap<K, V>.
  • This class makes no guarantees as to the order of the map.
  • It is similar to the Hashtable class except that it is unsynchronized and permits nulls(null values and null key).

Example

// Add values in HashMap
Map<Integer, String> hmap = new HashMap<Integer, String>();
hmap.put(1, "Hello");
hmap.put(null, "World");
hmap.put(null, null);
hmap.put(2, null);
hmap.put(3, null);

// Read value using EntrySet 
for (Map.Entry<Integer, String> entry : hmap.entrySet()) {
    System.out.println(entry.getKey() + " = " + entry.getValue());
}

/*
// Read value using Iterator
Iterator<Entry<Integer, String>> it = hmap.entrySet().iterator();
while(it.hasNext()){
    Entry<Integer, String> row = it.next();
    System.out.println(row.getKey() + " = " + row.getValue());
}
*/

// Output:
null = null
1 = Hello
2 = World
3 = null
4 = null
	

Above code simple shows how to add and retrieve values from a HashMap.

Based on output, Even though we tried adding multiple null key but only one null key is allowed.

This is not the case with value. As multiple null values are allowed in Hashmap.

Common methods
hmap.clear();  => The map will be empty after this call returns. Returns void.

hmap.containsKey(12); => Returns true if and only if this map contains a mapping for a key k such that (key==null ? k==null : key.equals(k))

hmap.containsValue("Hello"); => Returns true if this map maps one or more keys to the specified value

hmap.getOrDefault(12, "No Value"); => Returns the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key

hmap.size(); => Returns int the number of key-value mappings in this map . If the map contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.

hmap.isEmpty(); => Returns true if this map contains no key-value mappings

hmap.replace(12, "test"); => Replaces the entry for the specified key only if it is currently mapped to some value.

hmap.put(1, "Hello"); => Returns the previous value associated with key, or null if there was no mapping for key. (A null return can also indicate that the map previously associated null with key, if the implementation supports

Important Points

  • HashMap is unsynchronized, so its not thread safe and is not advisable to use in multi-threading Environment.
  • HashMap permits one null key and multiple null values.
  • Pay extra attention to methods provided by HashMap. There input and return type.
How null key is handled in HashMap?
  • Since equals() and hashCode() are used to store and retrieve values, how does it work in case of null key?
  • Null key is handled specially in HashMap, there are two separate method for that putForNullKey(V value) and getForNullKey()
  • Null keys always map to index 0.
  • Here is how nulls are retreived from HashMap
private V getForNullKey() {
	if (size == 0) {
		return null;
	}
	for (Entry<K,V> e = table[0]; e != null; e = e.next) {
		if (e.key == null)
			return e.value;
	}
	return null;
}
Internal Structure of Hashmap

Internally HashMap contains an array of Node. Node is represented as a class which contains 4 fields :

  • int hash
  • K key
  • V value
  • Node next

HashMap in Java works on Hashing Technique.