Java HashMap
In this page:
Introduction to HashMap
HashMap stores key-value pairs and uses each key's hashCode() to place entries into internal buckets, giving average O(1) lookup, insertion, and removal regardless of how many entries it holds.
Example: Introduction to HashMap
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<>();
map.put("apple", 3);
System.out.println(map.get("apple"));
}
}
Login to try C/C++/Java/PHP code in the editor
Removing and Clearing Elements
remove(key) deletes a single mapping by key, while clear() empties the entire map at once — both operations invalidate the map's size immediately without needing to rebuild it.
Example: Removing and Clearing Elements
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<>();
map.put("apple", 3);
map.put("banana", 5);
map.remove("apple");
System.out.println(map);
map.clear();
System.out.println(map.size());
}
}
Login to try C/C++/Java/PHP code in the editor
Iterating over HashMap
Iterating a HashMap typically means looping over entrySet() to get both key and value together, or keySet()/values() when you only need one side — HashMap makes no guarantee about the order entries come back in.
Example: Iterating over HashMap
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<>();
map.put("apple", 3);
map.put("banana", 5);
for (Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey() + "=" + entry.getValue());
}
}
}
Login to try C/C++/Java/PHP code in the editor
Updating Values
Calling put() with a key that already exists overwrites its previous value and returns the old one, which is a convenient way to both update a value and check what it used to be in a single call.
Example: Updating Values
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<>();
map.put("apple", 3);
Integer old = map.put("apple", 10); // overwrites, returns old value
System.out.println(old);
System.out.println(map.get("apple"));
}
}
Login to try C/C++/Java/PHP code in the editor
Useful HashMap Methods
Beyond basic get/put, HashMap offers getOrDefault() to avoid null checks, putIfAbsent() to avoid overwriting, and merge() for combining an existing value with a new one in one step — all of which reduce boilerplate for common update patterns.
Example: Useful HashMap Methods
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<>();
map.put("apple", 3);
System.out.println(map.getOrDefault("banana", 0));
map.putIfAbsent("apple", 99); // won't overwrite
map.merge("apple", 2, Integer::sum);
System.out.println(map.get("apple"));
}
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 17 topics to unlock
0/17 topics done
Complete these topics first:
- Java Data Structures
- Java Collections Introduction
- Java List Interface
- Java ArrayList
- Java LinkedList
- Java Map Interface
- Java HashMap
- Java Set Interface
- Java TreeMap
- Java LinkedHashMap
- Java HashSet
- Java TreeSet
- Java LinkedHashSet
- Java Stack & Queue
- Java Iterator
- Java Collections Class
- Java List Sorting