
Java HashMap
✨What is a HashMap in Java?
A HashMap
in Java is a part of the java.util
package that allows storage of key-value pairs. It provides fast retrieval, insertion, and deletion operations by using hashing techniques. HashMap is widely used when quick lookups are needed based on unique keys.
✨Features of Java HashMap:
- Key-Value Storage: HashMap stores data as key-value pairs where each key is unique, and the corresponding value can be retrieved using the key.
- No Duplicate Keys: Each key in a HashMap must be unique. If a duplicate key is used, the latest value replaces the previous one.
- Allows Null Values: HashMap allows one
null
key and multiplenull
values. - Non-Synchronized: HashMap is not thread-safe by default; for thread-safety, use
Collections.synchronizedMap()
. - Uses Object References: It does not support primitive types like
int
ordouble
. Instead, use wrapper classes likeInteger
andDouble
.
✨Declaring a HashMap in Java:
You can declare a HashMap using the following syntax:
// Generic type with String keys and Object values
HashMap<String, Object> map = new HashMap<>();
// Raw type declaration
HashMap map = new HashMap();
✨Commonly Used HashMap Methods:
put(K key, V value)
: Inserts a key-value pair into the map.get(Object key)
: Retrieves the value associated with the specified key.remove(Object key)
: Removes the mapping for the specified key.containsKey(Object key)
: Checks if a key exists in the map.containsValue(Object value)
: Checks if a value exists in the map.keySet()
: Returns a set of all keys.values()
: Returns a collection of all values.size()
: Returns the number of key-value mappings.isEmpty()
: Checks if the HashMap is empty.
✨Java HashMap Example:
Here’s a simple example demonstrating how to create and use a HashMap in Java:
import java.util.HashMap;
import java.util.Map;
public class HashMapExample {
public static void main(String[] args) {
Map<String, String> carDetails = new HashMap<>();
carDetails.put("Brand", "Toyota");
carDetails.put("Model", "Camry");
carDetails.put("Year", "2022");
carDetails.put("Price", "$30,000");
System.out.println("Car Details: " + carDetails);
}
}
Output:
Car Details: {Brand=Toyota, Model=Camry, Year=2022, Price=$30,000}
✨Removing Elements from a HashMap:
The remove()
method allows you to delete a specific key-value pair from the HashMap:
import java.util.HashMap;
public class RemoveKeyExample {
public static void main(String[] args) {
HashMap<Integer, String> courses = new HashMap<>();
courses.put(1, "Java");
courses.put(2, "Python");
courses.put(3, "PHP");
courses.put(4, "JavaScript");
System.out.println("Before Removal: " + courses);
courses.remove(3); // Removing PHP
System.out.println("After Removal: " + courses);
}
}
Output:
Before Removal: {1=Java, 2=Python, 3=PHP, 4=JavaScript}
After Removal: {1=Java, 2=Python, 4=JavaScript}
✨Checking for Keys and Values in a HashMap:
Checking if a Key Exists:
Use containsKey()
to check whether a key is present in the HashMap:
System.out.println(courses.containsKey(2)); // Output: true
Checking if a Value Exists:
Use containsValue()
to check whether a value is present:
System.out.println(courses.containsValue("Python")); // Output: true
✨Retrieving All Keys and Values:
Get All Keys:
System.out.println(courses.keySet()); // Output: [1, 2, 4]
Get All Values:
System.out.println(courses.values()); // Output: [Java, Python, JavaScript]
✨Conclusion:
HashMap in Java is a powerful data structure that provides an efficient way to store and retrieve data. Understanding its methods and operations helps optimize application performance and improves data handling efficiency. Use HashMap whenever you need quick lookups and flexible key-value storage in Java applications.