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