Core Java

LinkedHashMap In Java

Introduction:

LinkedHashMap is a Map implementation which maintains the insertion order of its elements. It extends the HashMap class:

Just like a HashMap, it allows storing one null key and many null values.

Instantiating LinkedHashMap:

We can use one of the following constructors to create a LinkedHashMap:

The very last constructor also accepts a boolean argument accessOrder.

When we pass true to the accessOrder, we’re instantiating an access-order LinkedHashMap. An access-order LinkedHashMap maintains the access order of elements from the least-recently-used to the most-recently-used, instead of the insertion order.

Types Of LinkedHashMap:

We can classify a Java LinkedHashMap in two main categories:

1. Insertion-Order LinkedHashMap:

Unless stated otherwise, a LinkedHashMap maintains the insertion order of its elements. We can name it as an insertion-order LinkedHashMap.

Let’s look at an example:

2. Access-Order LinkedHashMap:

When we use the constructor: LinkedHashMap(int initialCapacity, float loadFactor, boolean accessOrder) and pass a true value to the accessOrder argument, we’ll have an access-order LinkedHashMap.

Let’s see how an access-order LinkedHashMap actually works:

On accessing the element with the key as 2, it moved to the very last position. Clearly, the elements are ordered based on their access order from the least recent to the most recent.

Commonly Used Methods:

Let’s explore the popular methods:

  1. V put(K key, V value): inherits this method from the HashMap class. It stores the given key-value pair. If the key already exists, the value is updated
  2. void replaceAll(BiFunction<? super K, ? super V, ? extends V> function): replaces each entry’s value with the result of invoked function on that entry until all entries have been processed or the function throws an exception
  3. boolean containsKey(Object key): to check if a given key exists
  4. boolean containsValue(Object value): returns true if there’s at least one key with that value
  5. V get(Object key): to retrieve a value based on key
  6. V getOrDefault(Object key, V defaultValue): returns the mapped value if the key exists or else returns the default value
  7. void clear(): to remove all the elements
  8. Set<Map.Entry<K, V>> entrySet(): returns a Set view of the mappings in the given map
  9. Set<K> keySet(): returns a Set view of keys contained in the map
  10. Collection<V> values(): returns a Collection view of values contained in the map
  11. forEach(BiConsumer<? super K, ? super V> action): performs a given action on each entry in the given map until either all entries are processed or an exception gets thrown
  12. protected boolean removeEldestEntry(Map.Entry<K< V> eldest): this method will return true if the map should remove its eldest entry

Let’s now try out a few of these methods:

 Conclusion:

In this quick article, we looked at what a LinkedHashMap is and how can we instantiate and use it.

Be the First to comment.

Leave a Comment

Your email address will not be published. Required fields are marked *