Java HashMap Class Methods

The HashMap class in Java is a part of the Java Collections Framework and implements the Map interface. It is used for storing data in key-value pairs and provides an efficient way of retrieving values based on their keys. HashMap does not maintain any order of its entries, which makes it faster for many operations.

This guide covers various methods available in the HashMap class, offering a comprehensive understanding of how to manipulate and interact with hash maps in Java. These methods are crucial for efficient coding practices and help in performing operations like adding, removing, and checking for key-value pairs.

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

HashMap Class Methods

Method Description
put() Adds a key-value pair to the HashMap.
clear() Removes all key-value pairs from the HashMap.
containsKey() Checks if the HashMap contains a specific key.
containsValue() Checks if the HashMap contains a specific value.
get() Retrieves the value associated with a specific key.
isEmpty() Checks if the HashMap is empty.
putAll() Adds all key-value pairs from another map to the HashMap.
remove() Removes the key-value pair for a specific key.
size() Returns the number of key-value pairs in the HashMap.
entrySet() Returns a set view of the key-value pairs in the HashMap.
keySet() Returns a set view of the keys in the HashMap.
getOrDefault() Returns the value for a key, or a default value if the key is not found.
putIfAbsent() Adds a key-value pair if the key is not already present.
replace() Replaces the value for a specific key only if it is already mapped to a value.
forEach() Performs an action for each key-value pair in the HashMap.
compute() Computes a new value for a specific key using a given mapping function.
computeIfAbsent() Computes a value for a specific key if it is not already present.
merge() Merges the value for a key using a given mapping function if the key is already present.
valueSpliterator() Returns a spliterator over the values in the HashMap.
keySpliterator() Returns a spliterator over the keys in the HashMap.
entrySpliterator() Returns a spliterator over the key-value pairs in the HashMap.
values() Returns a collection view of the values in the HashMap.

Leave a Comment

Scroll to Top