Java HashMap Methods

About This Index
This page is the Java HashMap Methods index. Each row links to a full guide with syntax, parameters, return value, three runnable examples, and FAQs. Every example is a complete Java class with a main method and fixed data, and all of them compile on Java 8 and later.
Learn what a HashMap is on the HashMap hub. Use this page when you need a specific method such as put(), get(), or merge().
A HashMap prints and iterates its entries in the order of its internal buckets, not in insertion or alphabetical order, and that order can change as the map grows. Examples that print a whole map show the typical output on OpenJDK 8–21 and say so; many examples print through a sorted TreeMap copy instead.
How to Call a HashMap Method
Every method here is an instance method, so you call it on your map variable:
import java.util.HashMap;
import java.util.Map;
Map<String, Integer> marks = new HashMap<>();
marks.put("Ann", 81); // add: returns null
marks.put("Ann", 85); // replace: returns 81
int a = marks.get("Ann"); // 85
int z = marks.getOrDefault("Zed", 0); // 0
marks.merge("Bo", 1, Integer::sum); // Bo=1
boolean has = marks.containsKey("Bo"); // true Java HashMap Methods Table
All method guides in this Java tutorial series (22 tutorials), grouped by purpose.
Add and update
5 methodsStore a value under a key, copy a whole map, add only when a key is free, or change existing values one at a time or all at once.
| Method | Description | Tutorial |
|---|---|---|
put() | Add a mapping or overwrite an existing value. | Try it |
putAll() | Copy every mapping from another map. | Try it |
putIfAbsent() | Add a mapping only when the key is missing. | Try it |
replace() | Change a value only if the key already exists. | Try it |
replaceAll() | Replace every value using a function. | Try it |
Compute and merge
4 methodsJava 8 methods that build the new value with a function. They are the usual tools for counters, grouping, and caches; a null result removes the key (computeIfAbsent never removes).
| Method | Description | Tutorial |
|---|---|---|
compute() | Compute a new value from the key and its current value. | Try it |
computeIfAbsent() | Create and store a value only when the key is missing. | Try it |
computeIfPresent() | Update a value only when the key is present. | Try it |
merge() | Insert a value, or combine it with the existing one. | Try it |
Read and check
4 methodsLook up a value, fall back to a default, or test whether a key or a value is present. Key lookups are fast; containsValue scans every entry.
| Method | Description | Tutorial |
|---|---|---|
get() | Look up the value stored for a key. | Try it |
getOrDefault() | Read a value, or a fallback when the key is missing. | Try it |
containsKey() | Check whether a key is present. | Try it |
containsValue() | Check whether any key maps to a value. | Try it |
Views and iteration
4 methodsLive views of the keys, the values, or the key/value pairs, plus a lambda-based loop. All of them follow the same unguaranteed hash order.
Remove and copy
3 methodsDelete one mapping (optionally only when its value matches), delete everything, or make a shallow copy.
Size
2 methodsCount the mappings or check whether there are none.
Frequently Asked Questions
Start with put()
Store your first key/value pairs and see what put() returns.
12 people found this page helpful
