HashMap in Java
A HashMap in Java is part of the Java Collections Framework and provides the basic implementation of the Map interface. A HashMap stores data in key-value pairs, allowing efficient retrieval of values based on their corresponding keys. Here are some key characteristics of a HashMap:
- Hashing: Internally, a
HashMapuses an array of buckets. Each bucket can hold multiple key-value pairs, which are stored in linked lists. The position of each key-value pair in the array is determined by the hash code of the key. - Unique Keys: Each key in a
HashMapmust be unique. If you try to insert a duplicate key, the old value associated with that key will be replaced. - Null Values: A
HashMapallows null values and the null key.
Here’s an example demonstrating how to use a HashMap in Java:
import java.util.HashMap;
public class HashMapExample {
public static void main(String[] args) {
// Creating a HashMap
HashMap<String, Integer> map = new HashMap<>();
// Adding key-value pairs to the HashMap
map.put("Alice", 30);
map.put("Bob", 25);
map.put("Charlie", 35);
// Retrieving a value based on key
int ageOfAlice = map.get("Alice");
System.out.println("Alice's age: " + ageOfAlice);
// Checking if a key exists
if (map.containsKey("Bob")) {
System.out.println("Bob is in the map.");
}
// Checking if a value exists
if (map.containsValue(35)) {
System.out.println("There is someone who is 35 years old.");
}
// Iterating over key-value pairs
for (String key : map.keySet()) {
System.out.println("Key: " + key + ", Value: " + map.get(key));
}
// Removing a key-value pair
map.remove("Charlie");
// Checking the size of the HashMap
System.out.println("Size of the map: " + map.size());
}
}
Explanation of HashMap with Example
- Creating a HashMap:
HashMap<String, Integer> map = new HashMap<>();- Creates a
HashMapwhere keys are of typeStringand values are of typeInteger.
- Creates a
- Adding Key-Value Pairs:
map.put("Alice", 30);- Adds a key-value pair to the
HashMap. The key is"Alice"and the value is30.
- Adds a key-value pair to the
- Retrieving a Value:
map.get("Alice");- Retrieves the value associated with the key
"Alice", which is30.
- Retrieves the value associated with the key
- Checking for a Key:
map.containsKey("Bob");- Checks if the key
"Bob"exists in theHashMap.
- Checks if the key
- Checking for a Value:
map.containsValue(35);- Checks if the value
35exists in theHashMap.
- Checks if the value
- Iterating over Key-Value Pairs:
- Uses a for-each loop to iterate over the keys of the
HashMapand print each key-value pair.
- Uses a for-each loop to iterate over the keys of the
- Removing a Key-Value Pair:
map.remove("Charlie");- Removes the key-value pair with the key
"Charlie"from theHashMap.
- Removes the key-value pair with the key
- Checking the Size of the HashMap:
map.size();- Returns the number of key-value pairs in the
HashMap.
- Returns the number of key-value pairs in the
This example demonstrates basic operations such as adding, retrieving, checking, iterating, and removing key-value pairs in a HashMap.