Java HashMap: A Comprehensive Guide
Java HashMap: A Comprehensive Guide
In Java, a HashMap stores data as key‑value pairs, enabling fast retrieval of values by unique keys. Unlike arrays, the keys can be any object (except null), and the map automatically manages hashing and collision resolution.
Key Features
- Associates a unique key with a value, allowing
get()andput()operations. - Throws
NoSuchElementExceptionwhen a requested key does not exist. - Stores object references; primitive types must be wrapped (e.g.,
Integer,Double).
Declaring a HashMap
HashMap<String, Object> map = new HashMap<String, Object>();
HashMap x = new HashMap();
Essential Methods
get(Object key)– returns the value for the specified key.put(Object key, V value)– inserts or updates the key‑value pair.remove(Object key)– deletes the mapping for the specified key.containsKey(Object key)– checks if the key exists.keySet()– returns aSetof all keys.values()– returns aCollectionof all values.isEmpty()– tests whether the map has any entries.
Practical Example
Below is a complete, runnable snippet that demonstrates common operations:
import java.util.HashMap;
import java.util.Map;
public class SampleTestMaps {
public static void main(String[] args) {
Map<String, String> objMap = new HashMap<>();
objMap.put("Name", "Suzuki");
objMap.put("Power", "220");
objMap.put("Type", "2-wheeler");
objMap.put("Price", "85000");
System.out.println("Elements of the Map:");
System.out.println(objMap);
}
}
Output:
{Type=2-wheeler, Price=85000, Power=220, Name=Suzuki}
Removing a Specific Entry
import java.util.HashMap;
public class HashMapExample {
public static void main(String[] args) {
HashMap<Integer, String> map = new HashMap<>();
map.put(1, "Java");
map.put(2, "Python");
map.put(3, "PHP");
map.put(4, "SQL");
map.put(5, "C++");
System.out.println("Before removal: " + map);
map.remove(5);
System.out.println("After removal: " + map);
}
}
Output:
Before removal: {1=Java, 2=Python, 3=PHP, 4=SQL, 5=C++}
After removal: {1=Java, 2=Python, 3=PHP, 4=SQL}
Quick Reference
- Check existence:
containsKey(key) - List keys:
keySet() - List values:
values() - Remove entry:
remove(key) - Is empty?
isEmpty()
With these fundamentals, you can confidently integrate HashMaps into any Java application for efficient data storage and retrieval.
Java
- Encapsulation in Java: A Comprehensive Guide with Practical Example
- Understanding Java Classes and Objects: Clear Concepts, Practical Examples
- Understanding Java String.charAt(): Syntax, Return Type, Exceptions, and a Practical Example
- Mastering Java’s String.endsWith(): How to Check String Suffixes with Examples
- Command‑Line Arguments in Java: A Practical Guide with Code Examples
- Java Inheritance Explained: Types, Syntax, and Practical Examples
- Java Abstraction: Mastering Abstract Classes, Methods, and Practical Examples
- Understanding Java's throws Keyword: Examples & Best Practices
- Java Switch‑Case Statement Explained: Syntax, Examples, and Best Practices
- Mastering Java's split() Method: A Practical Guide with Code Examples