Java LinkedHashMap Class Methods

The LinkedHashMap class in Java is part of the java.util package and extends HashMap with a predictable iteration order. It maintains a doubly linked list running through all of its entries, providing a predictable iteration order based on the order in which keys were inserted. This makes it particularly useful when you need to iterate over elements in the order they were added.

This guide covers the various methods available in the LinkedHashMap class. Each method is described in simple terms to help beginners understand how to use them. The methods enable you to perform operations such as adding, removing, and accessing elements while preserving their order.

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

Learn Java LinkedHashMap: Java LinkedHashMap Tutorial.

Java LinkedHashMap Class Methods

The table below contains various methods of the Java LinkedHashMap 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() Inserts the specified key-value pair into the map.
clear() Removes all key-value pairs from the map.
clone() Returns a shallow copy of this LinkedHashMap instance.
containsKey() Checks if the map contains the specified key.
get() Returns the value to which the specified key is mapped.
isEmpty() Checks if the map is empty.
putAll() Copies all of the mappings from the specified map to this map.
remove() Removes the mapping for the specified key from the map.
size() Returns the number of key-value pairs in the map.
entrySet() Returns a Set view of the mappings contained in this map.
keySet() Returns a Set view of the keys contained in this map.
values() Returns a Collection view of the values contained in this map.
getOrDefault() Returns the value to which the specified key is mapped or defaultValue if this map contains no mapping for the key.
replace() Replaces the entry for the specified key only if it is currently mapped to some value.
replace(K key, V value) Replaces the entry for the specified key only if it is currently mapped to a given value.
forEach() Performs the given action for each entry in the map until all entries have been processed or the action throws an exception.
replaceAll() Replaces each entry's value with the result of applying the given function to that entry.
computeIfAbsent() If the specified key is not already associated with a value, it attempts to compute its value using the given mapping function and enters it into this map unless it is null.
merge() If the specified key is not already associated with a value or is associated with null, associate it with the given non-null value. Otherwise, replace the value with the results of the given remapping function or remove it if the result is null.
keySpliterator() Creates a late-binding and fail-fast Spliterator over the keys in this map.
valueSpliterator() Creates a late-binding and fail-fast Spliterator over the values in this map.
entrySpliterator() Creates a late-binding and fail-fast Spliterator over the entries in this map.
valueStream() Returns a sequential Stream with this map's values as its source.
entryStream() Returns a sequential Stream with this map's entries as its source.

Comments