← Back to Core Java Course | Chapter 11: Collections | Lesson 7 of 17

Java HashMap

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

java
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"));
	}
}

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

java
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());
	}
}

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

java
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());
		}
	}
}

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

java
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"));
	}
}

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

java
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 run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.