HashMap is a part of the Java Collection Framework that stores data in key-value pairs. It allows fast data retrieval using hashing technique and does not maintain insertion order.
null key and multiple null valuesHashMap belongs to java.util package and implements the Map interface. It uses hash table internally to store data.
// Creating a HashMap with Integer keys and String values
HashMap<Integer, String> map = new HashMap<>();
// Basic operations on HashMap
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
HashMap<Integer, String> students = new HashMap<>();
students.put(101, "Amit");
students.put(102, "Rahul");
students.put(103, "Sneha");
System.out.println(students);
System.out.println(students.get(102));
students.remove(101);
System.out.println(students);
}
}
Enter a key and value to see how put() and remove() update the map dynamically.
The HashMap prints all key-value pairs. The get() method fetches the value using the key, and remove() deletes the specified entry.
equals() and hashCode() properlyConcurrentHashMap for multithreadingcontainsKey()entrySet()replace()