Java LinkedHashMap Class Methods

The LinkedHashMap class in Java is a part of the Java Collections Framework and extends the HashMap class. It maintains a doubly-linked list running through all of its entries, which defines the iteration ordering. This ordered version of the HashMap allows predictable iteration order, which is determined by the order in which entries were inserted.

This guide covers various methods available in the LinkedHashMap class, offering a comprehensive understanding of how to manipulate and interact with linked 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.

LinkedHashMap Class Methods

Method Description
put() Adds a key-value pair to the LinkedHashMap.
clear() Removes all key-value pairs from the LinkedHashMap.
clone() Creates a shallow copy of the LinkedHashMap.
containsKey() Checks if the LinkedHashMap contains a specific key.
get() Retrieves the value associated with a specific key.
isEmpty() Checks if the LinkedHashMap is empty.
putAll() Adds all key-value pairs from another map to the LinkedHashMap.
remove(Object key) Removes the key-value pair for a specific key.
size() Returns the number of key-value pairs in the LinkedHashMap.
entrySet() Returns a set view of the key-value pairs in the LinkedHashMap.
keySet() Returns a set view of the keys in the LinkedHashMap.
values() Returns a collection view of the values in the LinkedHashMap.
getOrDefault() Returns the value for a key, or a default value if the key is not found.
replace(K key, V oldValue, V newValue) Replaces the value for a specific key only if it is already mapped to a specific value.
replace(K key, V value) 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 LinkedHashMap.
replaceAll() Replaces each entry’s value with the result of applying the given function to that entry.
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.
keySpliterator() Returns a spliterator over the keys in the LinkedHashMap.
valueSpliterator() Returns a spliterator over the values in the LinkedHashMap.
entrySpliterator() Returns a spliterator over the key-value pairs in the LinkedHashMap.
valueStream() Returns a sequential stream with the values in the LinkedHashMap as its source.
entryStream() Returns a sequential stream with the key-value pairs in the LinkedHashMap as its source.

Leave a Comment

Scroll to Top