Java HashMap Methods

Beginner
6 min read
Updated: Nov 2026
22 method guides
Method index

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().

No guaranteed order.

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:

Java
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 methods

Store 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.

MethodDescriptionTutorial
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 methods

Java 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).

MethodDescriptionTutorial
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 methods

Look 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.

MethodDescriptionTutorial
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 methods

Live 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.

MethodDescriptionTutorial
keySet()Get a live Set view of all keys.Try it
values()Get a live Collection view of all values.Try it
entrySet()Get a live Set view of key/value pairs.Try it
forEach()Run an action for every key and value.Try it

Remove and copy

3 methods

Delete one mapping (optionally only when its value matches), delete everything, or make a shallow copy.

MethodDescriptionTutorial
remove()Delete a mapping by key, or by key and value.Try it
clear()Remove every mapping from the map.Try it
clone()Make a shallow copy of the map.Try it

Size

2 methods

Count the mappings or check whether there are none.

MethodDescriptionTutorial
size()Count the key/value mappings.Try it
isEmpty()Check whether the map has no mappings.Try it

Frequently Asked Questions

They are the methods of java.util.HashMap, a hash table that maps keys to values. They add and update mappings, look up values by key, compute new values with functions, give views of the keys and values, and remove or copy mappings.
This index lists 22 guides, from put() and get() to computeIfAbsent(), merge(), and entrySet(), grouped by what they do.
compute(), computeIfAbsent(), computeIfPresent(), merge(), forEach(), getOrDefault(), putIfAbsent(), replace(), replaceAll(), and the two-argument remove(key, value). They are default methods of Map that HashMap overrides.
A HashMap stores entries in buckets chosen from the keys' hash codes and prints them bucket by bucket. The order is not guaranteed and can change as the map grows. Use LinkedHashMap for insertion order or TreeMap for sorted keys.
Read the Java HashMap hub first. Then open put(), get(), containsKey(), and merge() from the tables below.

Start with put()

Store your first key/value pairs and see what put() returns.

put() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful