The table below lists all HashMap methods.
Some methods use the HashMap’s entry types as parameters or return values. In the table, K represents the key type and V represents the value type.
Method |
Description |
Return Type |
clear() |
Remove all entries from the map. |
void |
clone() |
Make a copy of the HashMap. |
Object |
compute() |
Calculate a value for an entry using its key and the current value (if it exists). |
V |
computeIfAbsent() |
Calculate a value for an entry based on its key only if an entry with that key does not already exist. |
V |
computeIfPresent() |
Calculate a new value for an entry using its key and current value, but perform this operation only if an entry with the given key already exists. |
V |
containsKey() |
Determine whether an entry with the specified key exists in the map. |
boolean |
containsValue() |
Determine if an entry with the specified value exists in the map. |
boolean |
entrySet() |
Provide a set containing all entries in the map. |
Set< Map.Entry<K,V> > |
forEach() |
Execute an action for each entry in the map. |
void |
get() |
Retrieve the value associated with the specified key. |
V |
getOrDefault() |
Retrieve the value associated with the specified key, or return a default value if the entry is not found. |
V |
isEmpty() |
Determine if the map is empty. |
boolean |
keyset() |
Provide a set containing all keys in the map. |
Set<K> |
merge() |
Calculate a value for an entry using its key and value, or assign a specific value if the entry does not yet exist. |
V |
put() |
Add an entry to the map. |
V |
putAll() |
Copy all entries from another map into this one. |
void |
putIfAbsent() |
Add an entry to the map, but do so only if an entry with the same key does not already exist. |
V |
remove() |
Delete an entry from the map. |
V|boolean |
replace() |
Update an entry in the map only if it exists. |
V|boolean |
replaceAll() |
Replace the value of each entry with the result of an operation. |
void |
size() |
Provide the count of entries in the map. |
int |
values() |
Provide a collection containing all of the values in the map. |
Collection<V> |