Java Utility Library

Java.util.LinkedHashMap Class



Java LinkedHashMap Class

Java.util package provides a LinkedHashMap class which is a Hash table and linked list implementation of the Map interface, with predictable iteration order. This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries. The class provides all of the optional Map operations, and permits null elements. The Iteration over a HashMap is likely to be more expensive.

Class declaration

The declaration of java.util.LinkedHashMap class is:

public class LinkedHashMap<K,V>
  extends HashMap<K,V>
    implements Map<K,V>

Here, K and V are the type of key and value respectively maintained by the container.

Class Constructors

S.NConstructors & Description
1. LinkedHashMap()
Constructs an empty insertion-ordered LinkedHashMap instance with the default initial capacity (16) and load factor (0.75).
2. LinkedHashMap(int initialCapacity)
Constructs an empty insertion-ordered LinkedHashMap instance with the specified initial capacity and a default load factor (0.75).
3. LinkedHashMap(int initialCapacity, float loadFactor)
Constructs an empty insertion-ordered LinkedHashMap instance with the specified initial capacity and load factor.
4. LinkedHashMap(int initialCapacity, float loadFactor, boolean accessOrder)
Constructs an empty LinkedHashMap instance with the specified initial capacity, load factor and ordering mode.
5. LinkedHashMap(Map<? extends K,? extends V> m)
Constructs an insertion-ordered LinkedHashMap instance with the same mappings as the specified map.

java.util.LinkedHashMap Methods

The java.util.LinkedHashMap class has a number of methods which are listed below:

Member Methods

S.NMethods & Description
1. void clear()
Removes all of the mappings from this map.
2. boolean containsValue(Object value)
Returns true if this map maps one or more keys to the specified value.
3. Set<Map.Entry<K,V>> entrySet()
Returns a Set view of the mappings contained in this map.
4. void forEach(BiConsumer<? super K,? super V> action)
Performs the given action for each entry in this map until all entries have been processed or the action throws an exception.
5. V get(Object key)
Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
6. V getOrDefault(Object key, V defaultValue)
Returns the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key.
7. Set<K> keySet()
Returns a Set view of the keys contained in this map.
8. protected boolean removeEldestEntry(Map.Entry<K,V> eldest)
Returns true if this map should remove its eldest entry.
9. void replaceAll(BiFunction<? super K,? super V,? extends V> function)
Replaces each entry's value with the result of invoking the given function on that entry until all entries have been processed or the function throws an exception.
10. Collection<V> values()
Returns a Collection view of the values contained in this map.

Methods inherited

This class inherits the methods of following class:

  • java.lang.Object
  • java.util.AbstractMap<K,V>
  • java.util.HashMap<K,V>