Java HashMap Class Methods

The HashMap class in Java is part of the java.util package and is used to store data in key-value pairs. It is one of the most frequently used data structures because it provides average constant-time performance for the basic operations (get and put). The keys in a HashMap must be unique.

This guide provides an overview of the various methods available in the HashMap class. Each method is explained in simple terms, making it easy for beginners to understand how to use them. The methods allow you to add, remove, and search for key-value pairs, as well as perform various other operations on the map.

For more detailed information, you can refer to the official Java SE Documentation and additional resources on Java Collections Tutorial.

Java HashMap Class Methods

The table below contains various methods of the Java HashMap class, each with a link to a detailed explanation, examples, and real-world uses. Click on the method names to learn more about how to use them effectively in your applications.

Method Description
put() Adds a key-value pair to the map. If the key already exists, its value is updated.
clear() Removes all key-value pairs from the map.
containsKey() Checks if the map contains the specified key.
containsValue() Checks if the map contains the specified value.
get() Returns the value associated with the specified key.
isEmpty() Checks if the map is empty.
putAll() Adds all key-value pairs from the specified map to this map.
remove() Removes the key-value pair with the specified key from the map.
size() Returns the number of key-value pairs in the map.
entrySet() Returns a set view of the key-value pairs in the map.
keySet() Returns a set view of the keys contained in the map.
getOrDefault() Returns the value associated with the specified key, or a default value if the key is not found.
putIfAbsent() Inserts the specified key-value pair into the map if the key is not already present.
remove(Object key, Object value) Removes the key-value pair if it matches the specified key and value.
replace() Replaces the value for the specified key if it is currently mapped to some value.
forEach() Performs the given action for each key-value pair in the map.
compute() Computes a new mapping for the specified key using the given function.
computeIfAbsent() Computes a value for the specified key if it is not already present.
merge() Merges the value for the specified key using the given function if the key is already present.
keySpliterator() Returns a Spliterator over the keys in the map.
valueSpliterator() Returns a Spliterator over the values in the map.
entrySpliterator() Returns a Spliterator over the key-value pairs in the map.
keySet().stream() Returns a stream of the keys in the map.
entrySet().stream() Returns a stream of the key-value pairs in the map.
values() Returns a collection view of the values contained in the map.
replaceAll() Replaces each entry's value with the result of applying the given function to that entry.

Comments