HashMap
Deepak
Dec 21, 2024
HashMap is used to store data in a key-value manner. It provides a mapping between key and respected values.
HashMap < K, V > map = new HashMap
K (Key) : Keys maintained by Map of Type K
V(Value) : Mapped values of Type V
Key points of HashMap
- HashMap is part of the Collections Framework
- Found in java.util package Therefore required to import java.util.HashMap; before using HashMap.
- HashMap inherits the AbstractMap class
- HashMap implements the Map interface.
HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneable, Serializable
Scroll for More Useful Information and Relevant FAQs
- What is the difference between hashtable and hashmap?
- How to iterate hashmap in java? Learn number of ways to iterate hashmap.
- How to fetch value from hashmap by given key?
- what will hashmap return if mapping key does not exist?
- Feedback: Your input is valuable to us. Please provide feedback on this article.
What is the difference between hashtable and hashmap?
ID | Topic | Hashtable | HashMap |
---|---|---|---|
1 | Synchronization | Hashtable is synchronized, Meaning that in a multithreaded environment, it does not allow more than one thread to access and process the Hashtable at any particular time. | HashMap is not synchronized. In a multithreaded environment, there can be more than one thread that can access and process the same HashMap. |
2 | Null Key | Hashtable does not allow you to store any null key. | HashMap allows you to store one Null key. |
3 | Null Value | Hashtable does not allow you to store any null value. | HashMap allows you to store multiple null values. |
4 | Iteration | Traversing stored value can be achieved by using Enumerator and Iterator. In Hashtable is traversed by the Enumerator and Iterator. | Traversing stored value can be achieved by using an Iterator. |
5 | Inherits | It Inherits Dictionary class. | It inherits the AbstractMap class. |
6 | Thread-Safe | It is thread-safe. Also, two or more threads can share Hashtable. | It is not thread safe. It cannot be shared between many threads in general. It requires synchronization code to be shared. Example synchronizedMap: a method of Collections used to convert hashmap into synchronized map. |
7 | Execution Time | Slow than HashMap | HashMap is faster than Hashtable |
How to iterate hashmap in java? Learn number of ways to iterate hashmap.
Hashmap can be looped through by using
- For Loop Iteration using Iterator
- While Loop iteration with Iterator
- forEach Loop
- Stream
Go through How to Iterate (Loop) Hashmap in Java With Example for more details.
How to fetch value from hashmap by given key?
java.util.HashMap.get method use to fetch value from hashmap.
Here is the example code
package javaapplication;
import java.util.Map;
import java.util.HashMap;
public class JavaApplication {
public static void main(String[] args) {
// HashMap object with a list of employees with their name and department
Map<String, String> employees = new HashMap<>();
// keys and values (Name, Department)
employees.put("John", "HR");
employees.put("Eric", "IT");
employees.put("Joe", "LEGAL");
System.out.format("key (Name): %s, value (Department): %s %n", "Eric", employees.get("Eric"));
// Output
key (Name): Eric, value (Department): IT
}
}
what will hashmap return if mapping key does not exist?
null
It will return null by using java.util.Map.get() . Here is the example code.
// keys and values (Name, Department)
employees.put("John", "HR");
employees.put("Eric", "IT");
employees.put("Joe", "LEGAL");
System.out.format("key (Name): %s, value (Department): %s %n", "Eric", employees.get("noKey"));
Output: key (Name): Eric, value (Department): null