Java, Kotlin: HashMap Under the hood?

Leo N
5 min readOct 19, 2020

Definition

Java HashMap class is an implementation of Map interface based on hash table. It stores elements in key & value pairs which is denoted as HashMap<Key, Value> or HashMap<K, V>.

It extends AbstractMap class, and implements Map interface and can be accessed by importing java.util package. Declaration of this class is given below.

Java (java.util)

public class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneable, Serializable

Kotlin (kotlin.collections)

expect class HashMap<K, V> : MutableMap<K, V>

Create a HashMap:

// HashMap creation with 8 capacity and 0.6 load factor
HashMap<Key, Value> numbers = new HashMap<>(8, 0.6f);

Notice the part new HashMap<>(8, 0.6). Here, the first parameter is capacity and the second parameter is loadFactor.

  • load factor : The load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased. When the number of entries in the hash table…

--

--