Skillio, Pune’s Best IT Training and Placement Institute
HashMap in Java
A HashMap is part of the [Link] package. It is a widely used implementation of the Map
interface that stores key-value pairs.
Key Properties of HashMap
• Stores data in key-value pairs.
• Keys are unique, but values can be duplicate.
• Allows one null key and multiple null values.
• Unordered collection (no guarantee of insertion order).
• Backed by hashing technique.
• Initial capacity (default: 16) and load factor (default: 0.75).
• Performance: O(1) average for get() and put() operations.
• Not thread-safe. Use [Link]() or ConcurrentHashMap for
thread-safety.
• Rehashing occurs when the load factor threshold is crossed.
• Implements Serializable, Cloneable, and Map interfaces.
Basic Example
import [Link].*;
public class HashMapExample {
public static void main(String[] args) {
HashMap<Integer, String> map = new HashMap<>();
// Adding elements
[Link](1, "Apple");
[Link](2, "Banana");
[Link](3, "Cherry");
// Accessing element
[Link]("Value for key 2: " + [Link](2));
// Iterating over entries
for ([Link]<Integer, String> entry : [Link]()) {
[Link]([Link]() + " => " + [Link]());
}
}
}
Output:
Skillio, Pune +91-9130502135 | +91-8484831616
Skillio, Pune’s Best IT Training and Placement Institute
Value for key 2: Banana
1 => Apple
2 => Banana
3 => Cherry
Important Methods
• put(K key, V value) → Inserts mapping.
• get(Object key) → Returns value for key.
• remove(Object key) → Removes mapping.
• containsKey(Object key) → Checks if key exists.
• containsValue(Object value) → Checks if value exists.
• size() → Number of key-value pairs.
• isEmpty() → Checks if empty.
• clear() → Removes all entries.
Null Key and Value Example
import [Link].*;
public class NullExample {
public static void main(String[] args) {
HashMap<Integer, String> map = new HashMap<>();
[Link](null, "FirstNull");
[Link](2, null);
[Link](3, null);
[Link](map);
}
}
Skillio, Pune +91-9130502135 | +91-8484831616
Skillio, Pune’s Best IT Training and Placement Institute
Output:
{null=FirstNull, 2=null, 3=null}
Iteration Techniques
HashMap<String, Integer> scores = new HashMap<>();
[Link]("John", 90);
[Link]("Emma", 95);
[Link]("Alex", 85);
// For-each loop
for ([Link]<String, Integer> entry : [Link]()) {
[Link]([Link]() + " => " + [Link]());
}
// Using keySet()
for (String key : [Link]()) {
[Link](key + " => " + [Link](key));
}
// Using values()
for (Integer value : [Link]()) {
[Link]("Value: " + value);
}
Handling Duplicate Keys
HashMap<Integer, String> map = new HashMap<>();
[Link](1, "Apple");
[Link](1, "Orange");
[Link](map);
Output:
{1=Orange}
(Last inserted value overrides previous one.)
Custom Object as Key
When using objects as keys, override hashCode() and equals().
import [Link].*;
class Student {
Skillio, Pune +91-9130502135 | +91-8484831616
Skillio, Pune’s Best IT Training and Placement Institute
int id;
String name;
Student(int id, String name) {
[Link] = id;
[Link] = name;
}
@Override
public int hashCode() {
return id;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof Student)) return false;
Student s = (Student) obj;
return [Link] == [Link];
}
}
public class CustomKeyExample {
public static void main(String[] args) {
HashMap<Student, String> map = new HashMap<>();
Student s1 = new Student(101, "John");
Student s2 = new Student(101, "John");
[Link](s1, "First Entry");
[Link](s2, "Duplicate Entry");
[Link]([Link]());
}
}
Output:
1
Summary
• HashMap is efficient for storing key-value pairs.
• Average time complexity: O(1) for insertion, deletion, and lookup.
• Allows one null key and multiple null values.
• Not thread-safe.
Skillio, Pune +91-9130502135 | +91-8484831616
Skillio, Pune’s Best IT Training and Placement Institute
• Collisions are handled using chaining (linked list or tree in Java 8+).
• Proper implementation of hashCode() and equals() is critical when using objects
as keys.
Skillio, Pune +91-9130502135 | +91-8484831616