LinkedHashMap contains keys and values.
You can add a key-value.
All keys are unique (you cannot add duplicate keys).
Each key is associated with only one value, i.e. we get the value from the key.
You can change the value by key.
You can delete the key-value.
LinkedHashMap implementation the same as
HashMap and additionally maintains a doubly-linked list running through all of its elements. This linked list makes guarantees order of keys in the collection (this means that when you go through all the elements in
LinkedHashMap, you get the keys in the order that they were added to the collection)
LinkedHashMap is not synchronized. If multiple threads access a
LinkedHashMap concurrently, and at least one of the threads modifies the
LinkedHashMap, it must be synchronized externally.
LinkedHashMap
implements
interface Map<K,V> this collection contains keys and values
Java
Creating a new Java console application ... and write the code
package org.example;
import java.util.Stack;
public class Program {
public static void main(String[] args)
{
LinkedHashMap<String, Integer> mapNameAge = new LinkedHashMap<String, Integer>();
// Adding elements
mapNameAge.putLast("Jeck", 47);
mapNameAge.putLast("Iana", 14);
mapNameAge.putLast("Sveta", 42);
// "Jeck , 47"
// "Iana , 14"
// "Sveta , 42"
// check if the key is in the collection
Boolean hasElement = mapNameAge.containsKey("Iana");
// hasElement = true
// get value by key
Integer age1 = mapNameAge.get("Iana");
// age1 = 14
// Option 1. Go through all the elements
// When you go through all the elements in LinkedHashMap, the elements are obtained in the order that they were added to the collection.
for (Map.Entry<String, Integer> elem : mapNameAge.entrySet()) {
System.out.println(elem.getKey() + " , " + elem.getValue());
}
// "Jeck , 47"
// "Iana , 14"
// "Sveta , 42"
// Option 2. Go through all the elements
// When you go through all the elements in LinkedHashMap, the elements are obtained in the order that they were added to the collection.
mapNameAge.forEach((name,age) -> {
System.out.println(name + " , " + age);
});
// "Jeck , 47"
// "Iana , 14"
// "Sveta , 42"
// delete the element "Book"
mapNameAge.remove("Jeck");
// element1 = Iana, 14
// element2 = Sveta, 42
}
}
Java
class LinkedHashMap<K,V> extends HashMap<K,V> implements SequencedMap<K,V>
{
transient LinkedHashMap.Entry<K,V> head;
transient LinkedHashMap.Entry<K,V> tail;
final boolean accessOrder;
static class Entry<K,V> extends HashMap.Node<K,V> {
Entry<K,V> before, after;
Entry(int hash, K key, V value, Node<K,V> next) {
super(hash, key, value, next);
}
}
/* ---------------- Constructors -------------- */
public LinkedHashMap(int initialCapacity, float loadFactor) {
super(initialCapacity, loadFactor);
accessOrder = false;
}
public LinkedHashMap(int initialCapacity) {
super(initialCapacity);
accessOrder = false;
}
public LinkedHashMap() {
super();
accessOrder = false;
}
public LinkedHashMap(Map<? extends K, ? extends V> m) {
super();
accessOrder = false;
putMapEntries(m, false);
}
public LinkedHashMap(int initialCapacity, float loadFactor, boolean accessOrder) {
super(initialCapacity, loadFactor);
this.accessOrder = accessOrder;
}
/* ---------------- Methods -------------- */
public V putFirst(K k, V v) {
try {
putMode = PUT_FIRST;
return this.put(k, v);
} finally {
putMode = PUT_NORM;
}
}
public V putLast(K k, V v) {
try {
putMode = PUT_LAST;
return this.put(k, v);
} finally {
putMode = PUT_NORM;
}
}
...
}