Java Reference Library →

HashMap Methods Reference

HashMap Methods Reference

Explore the complete list of Java HashMap methods with usage syntax and simple explanations. Useful for Java developers working with key-value pairs.

This reference explains HashMap Methods Reference through its syntax, examples and boundaries. Use the linked course lessons when the surrounding Java concepts are unfamiliar.

  1. Use this reference

    Identify the documented behavior, read a complete example and check its inputs before applying it.

HashMap Methods Reference

clear()
public void clear()
Removes all mappings from the map.
clone()
public Object clone()
Returns a shallow copy of the HashMap.
compute()
public V compute(K key, BiFunction remappingFunction)
Attempts to compute a new mapping for the specified key.
computeIfAbsent()
public V computeIfAbsent(K key, Function mappingFunction)
Computes and stores a value for the key if it is not already present.
computeIfPresent()
public V computeIfPresent(K key, BiFunction remappingFunction)
Updates the value only if the key is already present.
containsKey()
public boolean containsKey(Object key)
Checks if the map contains the specified key.
containsValue()
public boolean containsValue(Object value)
Checks if the map maps one or more keys to the specified value.
entrySet()
public Set> entrySet()
Returns a set view of the key-value mappings.
forEach()
public void forEach(BiConsumer action)
Performs an action for each entry in the map.
get()
public V get(Object key)
Returns the value to which the specified key is mapped, or null.
getOrDefault()
public V getOrDefault(Object key, V defaultValue)
Returns the value for the key, or a default if the key is not present.
isEmpty()
public boolean isEmpty()
Checks if the map contains no key-value mappings.
keySet()
public Set keySet()
Returns a Set view of the keys contained in the map.
merge()
public V merge(K key, V value, BiFunction remappingFunction)
Merges the value with existing value using a mapping function.
put()
public V put(K key, V value)
Associates the specified value with the specified key in the map.
putAll()
public void putAll(Map m)
Copies all of the mappings from the specified map to this map.
putIfAbsent()
public V putIfAbsent(K key, V value)
Puts the value only if the key is not already associated with a value.
remove()
public V remove(Object key)public boolean remove(Object key, Object value)
Removes the mapping for the specified key or key-value pair.
replace()
public V replace(K key, V value)public boolean replace(K key, V oldValue, V newValue)
Replaces the entry for the specified key if it is currently mapped.
replaceAll()
public void replaceAll(BiFunction function)
Replaces each entry’s value using the provided function.
size()
public int size()
Returns the number of key-value mappings in the map.
values()
public Collection values()
Returns a Collection view of the values contained in the map.

Using this reference

Check the example’s types, inputs and documented behavior before copying it into an application. Some APIs require external libraries, files or platform services; consult their setup notes.

Related explanations: Java Data Types - Primitive and Non-Primitive, Java Class - Blueprint of Objects, Exceptions in Java Introduction.

Supporting documentation: Official Java learning guide.

Reference checks are optional and do not count toward course progress.

    Reference library →