Java : LinkedHashMap with Examples

LinkedHashMap (Java SE 18 & JDK 18) API Examples.
You will find code examples on most LinkedHashMap methods.


Summary

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.

Class diagram

final var map = new LinkedHashMap<String, Integer>();
System.out.println(map); // {}

map.put("a", 10);
map.put("b", 20);
map.put("c", 30);
System.out.println(map); // {a=10, b=20, c=30}

System.out.println(map.get("a")); // 10
System.out.println(map.get("b")); // 20
System.out.println(map.get("X")); // null

map.remove("b");
System.out.println(map); // {a=10, c=30}
final var linkedHashMap = new LinkedHashMap<String, Integer>();
linkedHashMap.put("xxx", 10);
linkedHashMap.put("yyy", 20);
linkedHashMap.put("aaa", 30);
linkedHashMap.put("bbb", 40);

System.out.println(linkedHashMap); // {xxx=10, yyy=20, aaa=30, bbb=40}

final var hashMap = new HashMap<String, Integer>();
hashMap.put("xxx", 10);
hashMap.put("yyy", 20);
hashMap.put("aaa", 30);
hashMap.put("bbb", 40);

System.out.println(hashMap); // {aaa=30, bbb=40, yyy=20, xxx=10}

Constructors

LinkedHashMap ()

Constructs an empty insertion-ordered LinkedHashMap instance with the default initial capacity (16) and load factor (0.75).

final var map = new LinkedHashMap<String, Integer>();
System.out.println(map); // {}
System.out.println(map.isEmpty()); // true

map.put("a", 10);
map.put("b", 20);
map.put("c", 30);

System.out.println(map); // {a=10, b=20, c=30}
System.out.println(map.size()); // 3

LinkedHashMap (int initialCapacity)

Constructs an empty insertion-ordered LinkedHashMap instance with the specified initial capacity and a default load factor (0.75).

final var map = new LinkedHashMap<String, Integer>(10000000);
System.out.println(map); // {}

final var startTime = System.nanoTime();

for (int i = 0; i < 1000000; i++) {
    map.put(String.valueOf(i), i);
}

final var endTime = System.nanoTime();

// 0.189023 sec.
System.out.printf("%f sec.%n", (endTime - startTime) / 1000000000.0);
final var map = new LinkedHashMap<String, Integer>(1);
System.out.println(map); // {}

final var startTime = System.nanoTime();

for (int i = 0; i < 1000000; i++) {
    map.put(String.valueOf(i), i);
}

final var endTime = System.nanoTime();

// 0.217200 sec.
System.out.printf("%f sec.%n", (endTime - startTime) / 1000000000.0);

LinkedHashMap (int initialCapacity, float loadFactor)

Constructs an empty insertion-ordered LinkedHashMap instance with the specified initial capacity and load factor.

final var map = new LinkedHashMap<String, Integer>(16, 0.05f);
System.out.println(map); // {}

final var startTime = System.nanoTime();

for (int i = 0; i < 1000000; i++) {
    map.put(String.valueOf(i), i);
}

final var endTime = System.nanoTime();

// 0.324748 sec.
System.out.printf("%f sec.%n", (endTime - startTime) / 1000000000.0);
final var map = new LinkedHashMap<String, Integer>(16, 0.75f);
System.out.println(map); // {}

final var startTime = System.nanoTime();

for (int i = 0; i < 1000000; i++) {
    map.put(String.valueOf(i), i);
}

final var endTime = System.nanoTime();

// 0.219669 sec.
System.out.printf("%f sec.%n", (endTime - startTime) / 1000000000.0);

LinkedHashMap (int initialCapacity, float loadFactor, boolean accessOrder)

Constructs an empty LinkedHashMap instance with the specified initial capacity, load factor and ordering mode.

// accessOrder = false
final var map = new LinkedHashMap<String, Integer>(16, 0.75f, false);
map.put("a", 10);
map.put("b", 20);
map.put("c", 30);

System.out.println(map); // {a=10, b=20, c=30}

System.out.println(map.get("a")); // 10
System.out.println(map); // {a=10, b=20, c=30}

System.out.println(map.get("b")); // 20
System.out.println(map); // {a=10, b=20, c=30}
// accessOrder = true
final var map = new LinkedHashMap<String, Integer>(16, 0.75f, true);
map.put("a", 10);
map.put("b", 20);
map.put("c", 30);

System.out.println(map); // {a=10, b=20, c=30}

System.out.println(map.get("a")); // 10
System.out.println(map); // {b=20, c=30, a=10}

System.out.println(map.get("b")); // 20
System.out.println(map); // {c=30, a=10, b=20}

LinkedHashMap (Map<? extends K,? extends V> m)

Constructs an insertion-ordered LinkedHashMap instance with the same mappings as the specified map.

final var m = Map.of("a", 10, "b", 20, "c", 30);
System.out.println(m); // {b=20, a=10, c=30}

final var map = new LinkedHashMap<>(m);
System.out.println(map); // {b=20, a=10, c=30}
System.out.println(map.size()); // 3

Methods

void clear ()

Removes all of the mappings from this map.

final var map = new LinkedHashMap<String, Integer>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);

System.out.println(map); // {a=1, b=2, c=3}
System.out.println(map.size()); // 3

map.clear();
System.out.println(map); // {}
System.out.println(map.size()); // 0

boolean containsValue (Object value)

Returns true if this map maps one or more keys to the specified value.

final var map = new LinkedHashMap<String, Integer>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
map.put("X", 3);

System.out.println(map); // {a=1, b=2, c=3, X=3}

System.out.println(map.containsValue(0)); // false
System.out.println(map.containsValue(1)); // true
System.out.println(map.containsValue(2)); // true
System.out.println(map.containsValue(3)); // true
System.out.println(map.containsValue(4)); // false

Set<Map.Entry<K,V>> entrySet ()

Returns a Set view of the mappings contained in this map.

final var map = new LinkedHashMap<String, Integer>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
System.out.println(map); // {a=1, b=2, c=3}

final var entries = map.entrySet();
System.out.println(entries); // [a=1, b=2, c=3]

map.replace("b", 20);
System.out.println(map); // {a=1, b=20, c=3}
System.out.println(entries); // [a=1, b=20, c=3]

entries.remove(Map.entry("a", 1));
System.out.println(map); // {b=20, c=3}
System.out.println(entries); // [b=20, c=3]

for (final var entry : entries) {
    entry.setValue(entry.getValue() * 10);
}

System.out.println(map); // {b=200, c=30}
System.out.println(entries); // [b=200, c=30]

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.

final var map = new LinkedHashMap<String, Integer>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);

System.out.println(map); // {a=1, b=2, c=3}

System.out.println(map.get("a")); // 1
System.out.println(map.get("b")); // 2
System.out.println(map.get("c")); // 3
System.out.println(map.get("X")); // null
System.out.println(map.get("")); // null

Set<K> keySet ()

Returns a Set view of the keys contained in this map.

final var map = new LinkedHashMap<String, Integer>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
System.out.println(map); // {a=1, b=2, c=3}

final var keys = map.keySet();
System.out.println(keys); // [a, b, c]

map.put("d", 4);
System.out.println(map); // {a=1, b=2, c=3, d=4}
System.out.println(keys); // [a, b, c, d]

keys.remove("a");
System.out.println(map); // {b=2, c=3, d=4}
System.out.println(keys); // [b, c, d]

protected boolean removeEldestEntry (Map.Entry<K,V> eldest)

Returns true if this map should remove its eldest entry.

class MyMap extends LinkedHashMap<String, Integer> {
    private static final int MAX_ENTRIES = 3;

    @Override
    protected boolean removeEldestEntry(Map.Entry<String, Integer> eldest) {
        System.out.println("removeEldestEntry eldest : " + eldest);
        return size() > MAX_ENTRIES;
    }
}

final var map = new MyMap();

System.out.println("-- put a --");
map.put("a", 10);
System.out.println("map : " + map);

System.out.println("-- put b --");
map.put("b", 20);
System.out.println("map : " + map);

System.out.println("-- put c --");
map.put("c", 30);
System.out.println("map : " + map);

System.out.println("-- put d --");
map.put("d", 40);
System.out.println("map : " + map);

System.out.println("-- put e --");
map.put("e", 50);
System.out.println("map : " + map);

// Result
// ↓
//-- put a --
//removeEldestEntry eldest : a=10
//map : {a=10}
//-- put b --
//removeEldestEntry eldest : a=10
//map : {a=10, b=20}
//-- put c --
//removeEldestEntry eldest : a=10
//map : {a=10, b=20, c=30}
//-- put d --
//removeEldestEntry eldest : a=10
//map : {b=20, c=30, d=40}
//-- put e --
//removeEldestEntry eldest : b=20
//map : {c=30, d=40, e=50}

Collection<V> values ()

Returns a Collection view of the values contained in this map.

final var map = new LinkedHashMap<String, Integer>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
System.out.println(map); // {a=1, b=2, c=3}

final var values = map.values();
System.out.println(values); // [1, 2, 3]

map.replace("b", 20);
System.out.println(map); // {a=1, b=20, c=3}
System.out.println(values); // [1, 20, 3]

values.remove(1);
System.out.println(map); // {b=20, c=3}
System.out.println(values); // [20, 3]

//values.add(4); // UnsupportedOperationException

Methods declared in HashMap

clear, clone, compute, computeIfAbsent, computeIfPresent, containsKey, isEmpty, merge, put, putAll, remove, size

Please see the link below.

Methods declared in AbstractMap

equals, hashCode, toString

Please see the link below.

Methods declared in Map

compute, computeIfAbsent, computeIfPresent, containsKey, equals, forEach, getOrDefault, hashCode, isEmpty, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, replaceAll, size

Please see the link below.


Related posts

To top of page