In computer programming terms, a map is a set of associations between pairs of objects. Java HashMap is the basic implementation of Map interface. Java provides two data structures for hashtables: one is Hashtable and next one is HashMap. HashMap is similar to Hashtable with two exceptions: HashMap methods are unsynchronized and it allows null key and null values unlike Hashtable. Hashtable is synchronized and performs poorly in a single threaded environment. Because of that HashMap is generally preferred, unless you have to deal with threads and synchronization. HashMap is not a Thread safe collection and need synchronization handled properly.
HashMap is a generic class, used for storing collection of the data in the form of key and value pairs and contains values based on the key. This HashMap implementation provides all sorts of the optional map operations, and permits null values and the null key. Moreover, it maintains no order.
Objects are stored by calling put(key, value) method of HashMap and retrieved by calling get(key) method.
How Java hashmap works?
HashMap works on the principle of Hashing. In simple, hashing is a way to assigning a unique code for any variable/object after applying any formula/algorithm on its properties. Hash function should return the same hash code each and every time, when function is applied on same or equal objects.
HashMap has a number of "buckets" which it uses to store key-value pairs in. A bucket is used to store multiple key value pairs . In hash map, bucket used simple linked list to store objects. Each bucket has a unique number , that's what identifies the bucket. When you put(key, value) into the map, the hashmap will look at the hash code of the key, and store the pair in the bucket of which the identifier is the hash code of the key. For example, the hash code of the key is 512 the pair is stored in bucket number 512. if there is any collision ,HashMap uses LinkedList to store object. It is important to note that, one bucket can store more then one key-value pair.
When you lookup a value in the hashmap, by giving it a key (get(key)), the hashcode determines which bucket for the hashmap to check. It will first look at the hash code of the key that you gave. The hashmap will then look into the corresponding bucket , and then it will compare the key that you gave with the keys of all pairs in the bucket, by comparing them with equals(). If there is more than one object in the bucket then a linear search is done to find which item in the bucket equals the desired item by using the equals() method.
How can I add elements in Hashmap?
How to get Size of Java HashMap?
The size() method is used to return the number of key-value mappings in this map.
How to iterate over the elements in Hashmap?
There are several ways you can view items in Hashmap.
Use entrySet() to iterate through Map and need to access value and key:
Using for loop:
Using iterator and Map.Entry:
Using foreach and Map.Entry:
Using while loop:
Removing entries from a HashMap
The remove() method is used to remove the mapping for the specified key from this map if present.
Remove all values from Java HashMap
How to search a key in HashMap?
By using containsKey() method you can find out the key existence.
How to get Key from Value in HashMap?
The following Java program illustrates all the above mentioned method in a single program