Java LinkedHashMap Methods Explained
In Java, LinkedHashMap is a class that extends HashMap, maintaining the order of key-value pairs based on insertion or access order (which can be specified through the constructor).
LinkedHashMap is primarily used in scenarios where iterating in insertion order or access order is needed, and it provides the following common methods:
- store the specified key-value pair in the LinkedHashMap.
- Retrieve the value corresponding to the specified key.
- Delete the key-value pair associated with the specified key.
- Check if the specified key is contained in the LinkedHashMap.
- clear(): removes all key-value pairs from the LinkedHashMap.
- keySet(): retrieve the set of all keys in the LinkedHashMap.
- values(): retrieve the collection of all values in the LinkedHashMap.
- Retrieve the collection of all key-value pairs in a LinkedHashMap using entrySet() method, and the return type is Set
>. - size(): obtain the number of key-value pairs in the LinkedHashMap.
- isEmpty(): Determines if the LinkedHashMap is empty.
- containsValue(value): Checks if the specified value is contained in the LinkedHashMap.
- Obtain the value associated with a specified key, returning a default value if the key does not exist.
- Iterate over the key-value pairs in the LinkedHashMap and perform a specified action on each pair.
- replace(key, value): substitute the value corresponding to the specified key.
- putAll(Map extends K, ? extends V> m): Store all key-value pairs from the specified Map into a LinkedHashMap.
In addition to the methods mentioned above, LinkedHashMap also offers some other methods for manipulating the key-value pairs in the LinkedHashMap.