Skip to content

Java LinkedHashMap Tutorial with Examples

Java LinkedHashMap tutorial with examples will help you understand how to use Java LinkedHashMap in an easy way. The LinkedHashMap in Java is an implementation of a hashtable and linked list of the Map interface. Unlike HashMap, LinkedHashMap maintains the order of its elements. That means it returns the elements in the same order they were inserted.

Another difference between HashMap and LinkedHashMap is that internally the LinkedHashMap class maintains a doubly linked list to link all of its elements and guarantees to maintain the insertion order of the elements. Plus reinserting the same key into the LinkedHashMap object does not change the element’s order.

Java LinkedHashMap Tutorial

Like HashMap, LinkedHashMap class in Java provides all Map operations and allows null elements. The LinkedHashMap class provides constant time performance for the basic operations like add, remove and contains.

LinkedHashMap in Java is not a synchronized implementation. That means if multiple threads are simultaneously accessing the LinkedHashMap and modifying it, the code must handle the synchronization explicitly. You can get a synchronized map instance from the LinkedHashMap object using the synchronizedMap method of the Collections class as given below.

You can also use the existing LinkedHashMap object instead of creating a new one.

How to create LinkedHashMap objects?

The LinkedHashMap class in Java provides several constructors to create new objects. The default no-argument constructor creates a new and empty LinkedHashMap object.

If you want to convert an existing map object like HashMap to LinkedHashMap, you can use the below given constructor which accepts a map object. This constructor creates a new LinkedHashMap object having the same key-value mappings as the specified map object.

There is also a special LinkedHashMap constructor which creates a new LinkedHashMap object that returns the elements in the order they were last accessed. To create such a LinkedHashMap object, you can use below given constructor.

This kind of map object is particularly useful to create a cache of the frequently accessed data.

LinkedHashMap Methods

Below given are some of the most important LinkedHashMap methods.

How to add key value mappings to the LinkedHashMap object using the put, putAll, and putIfAbsent methods?

The put method of the LinkedHashMap class adds a key-value mapping to the LinkedHashMap object. If the key exists, the previous value associated with the key is replaced by the specified new value the old value is returned. This method returns null if the specified key does not exist in the map or the key was associated with a null value.

Output

If you do not want to replace the value if the key already exists in the map, use the putIfAbsent method instead of the put method. The putIfAbsent method adds key-value mapping if and only if the key does not exist in the map or the key is associated with a null value.

If the key does not exist in the map or it is mapped to a null value, this method creates an association of the key with the given value and returns null. If the key already exists in the map, it returns a value associated with the key.

Output

The putAll method of the LinkedHashMap class adds all the key-value mappings of the specified map to this map object. Values with the matching keys will be replaced with the values contained in the specified map.

The below given example shows how to copy all mappings of HashMap to LinkedHashMap using the putAll method.

Output

As you can see from the output, the value for key “3” in LinkedHashMap is replaced by the value of the key “3” of the HashMap object.

How to get value mapped to a key from LinkedHashMap using the get and getOrDefault methods?

The get method of LinkedHashMap class returns the value associated with the given key. If there is no value associated with the given key, it returns null.

Output

The getOrDefault method is similar to the get method. It returns a value associated with the given key if the key exists. However, if the key is not mapped to any value, this method returns the specified default value.

Output

How to remove key value mappings from LinkedHashMap using the remove method?

The remove method of the LinkedHashMap class removes a key-value pair for the given key. It returns the value associated with the key or null if the mapping does not exist for the given key. Remember, the remove method may also return null if the specified key was associated with the null value.

Output

Use an overloaded remove method if you want to delete a mapping for a given key only if it associated with the specified value. It returns true if the key value mapping is removed from the map, false otherwise.

Output

How to remove all mappings from LinkedHashMap object using the clear method?

Use clear method of the LinkedHashMap class to remove all mappings from it. The LinkedHashMap object becomes empty after this method call.

Output

How to get the size of the LinkedHashMap object using the size method? (LinkedHashMap length)

The size method of the LinkedHashMap class returns the number of mappings stored in the map. It returns 0 if the LinkedHashMap is empty.

Output

How to check if the LinkedHashMap is empty using the isEmpty method?

The isEmpty method returns true if the map contains no key-value mappings. It returns false if there is at least one mapping in the map.

Output

You can also use the size method to check if the LinkedHashMap is empty. However, using the isEmpty method makes the code cleaner and readable.

How to get all keys of the LinkedHashMap using the keySet method?

The keySet method returns a Set view containing all the keys of the LinkedHashMap object. The key set returned by this method is backed by the original LinkedHashMap object. Any changes you make to the key set will be reflected in the original LinkedHashMap object, and vice versa.

Output

How to get all values of the LinkedHashMap using the values method?

The values method of the LinkedHashMap class returns a Collection containing all the values contained in the map object.

Output

Please note that the Collection returned by the values method is backed by the original map. Any changes made to the original LinkedHashMap object will reflect in the Collection. Similarly, any changes made to the Collection will reflect in the original map object.

How to get all entries or all mappings of LinkedHashMap using the entrySet method?

The entrySet method of the LinkedHashMap class returns the Set view of key-value mappings or entries contained in the map.

Output

The Set returned by this method is backed by the original map object. So any changes you make to this set will reflect in the original map, and vice versa.

How to check if the LinkedHashMap contains a key using the containsKey method?

The containsKey method of the LinkedHashMap class is used to check if the LinkedHashMap contains the specified key. It returns true if the key is associated with any value. It returns false if the key is not mapped to any value in the map.

Output

Note: If the LinkedHashHashMap key is an object of a custom class, the class must override the equals and hashCode methods for the containsKey method to work.

How to check if the LinkedHashMap contains a value using the containsValue method?

The containsValue method of the LinkedHashMap class returns true if the given value is associated with one or more keys.

Output

Note: If the LinkedHashHashMap value is an object of a custom class, the class must override the equals and hashCode methods for the containsValue method to work.

How to replace a value for a key in LinkedHashMap using the replace method?

The replace method of the LinkedHashMap class replaces a value for the specified key, only if the key is mapped to some value. If the key is not mapped to any value, this method does not create a new mapping like the put method.

Additionally, this method returns previously associated value with the key if the key is found in the map and value is replaced. It returns null if the value is not replaced or the key was mapped to a null value.

Output

You can also use an overloaded replace method which replaces the value for the specified key, only if the key is mapped to the specified value. If the key is not mapped to the specified value, the value is not replaced. This method returns true if the value replaced, false otherwise.

Output

How to iterate through LinkedHashMap object?

How to iterate through LinkedHashMap keys?

To iterate over all the keys of the LinkedHashMap object, use the keySet method as given below.

Output

How to iterate through LinkedHashMap values?

To iterate over all the values of the LinkedHashMap object, use the values method as given below.

Output

How to iterate through all LinkedHashMap mappings or entries?

To iterate over all the key value mappings of the LinkedHashMap object, use the entrySet method as given below.

Output

How to clone a LinkedHashMap object using the clone method?

The clone method of the LinkedHashMap class returns a shallow copy of this map object. Actual key and value objects are not copied, only their references are copied that is why it is called a shallow copy. Please refer to below given example to understand it in more detail.

Output

The below given additional LinkedHashMap examples will help you understand the concepts in more detail.

LinkedHashMap Examples

References:
Java LinkedHashMap Javadoc

Please let me know if you liked the Java LinkedHashMap tutorial with examples in the comments section below.

About the author

Leave a Reply

Your email address will not be published.