Understanding the differences between HashMap and LinkedHashMap

Understanding the differences between HashMap and LinkedHashMap

brief introduction

We know that the variable order of HashMap is unpredictable, which means that the convenient output order is not necessarily the same as the insertion order of HashMap.This feature is often confusing to our work.To do this, we can use LinkedHashMap.

LinkedHashMap Details

First look at the definition of LinkedHashMap:

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

LinkedHashMap inherits from HashMap, so all the functions of HashMap are available in LinkedHashMap.

The difference between LinkedHashMap and HashMap is that a new Entry is created:


    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);
        }
    }

This Entry inherits from HashMap.Node and has an extra before, after, to connect Nodes.

With this newly created Entry, you can ensure that the order of traversal is the same as the order of insertion.

insert

Here's an example of LinkedHashMap insertion:

    [@Test](https://my.oschina.net/azibug)
    public void insertOrder(){
        LinkedHashMap<String, String> map = new LinkedHashMap<>();
        map.put("ddd","desk");
        map.put("aaa","ask");
        map.put("ccc","check");
        map.keySet().forEach(System.out::println);
    }

Output results:

ddd
aaa
ccc

You can see that the output and insert results are consistent.

Visit

In addition to the order of traversal, LinkedHashMap has a very distinctive access order.

Let's look at one more constructor for LinkedHashMap:

    public LinkedHashMap(int initialCapacity,
                         float loadFactor,
                         boolean accessOrder) {
        super(initialCapacity, loadFactor);
        this.accessOrder = accessOrder;
    }

The first two parameters, initialCapacity, loadFactor, which we've talked about before, are accessOrder.

When accessOrder is set to true, access-order is turned on.

access order means that the order in which the oldest access to the latest access to an object installation is ordered.Let's take an example:

    [@Test](https://my.oschina.net/azibug)
    public void accessOrder(){
        LinkedHashMap<String, String> map = new LinkedHashMap<>(16, .75f, true);
        map.put("ddd","desk");
        map.put("aaa","ask");
        map.put("ccc","check");
        map.keySet().forEach(System.out::println);
        map.get("aaa");
        map.keySet().forEach(System.out::println);
    }

Output results:

ddd
aaa
ccc
ddd
ccc
aaa

As we can see, because we visited "aaa" once, we reached the end of the traversal.

removeEldestEntry

Finally, let's take a look at one of the special features of LinkedHashMap, removeEldestEntry.What is this method for?

By re-removeEldest Entry method, LinkedHashMap can save a specific number of Entries, which is often used when LinkedHashMap is used as a cache.

removeEldestEntry will delete the oldest Entry and keep the latest.

ublic class CustLinkedHashMap<K, V> extends LinkedHashMap<K, V> {

    private static final int MAX_ENTRIES = 10;

    public CustLinkedHashMap(
            int initialCapacity, float loadFactor, boolean accessOrder) {
        super(initialCapacity, loadFactor, accessOrder);
    }

    [@Override](https://my.oschina.net/u/1162528)
    protected boolean removeEldestEntry(Map.Entry eldest) {
        return size() > MAX_ENTRIES;
    }
}

Looking at a custom example above, we created a LinkedHashMap that retains 10 Entry nodes.

summary

LinkedHashMap inherits from HashMap and provides two very useful features.

Examples of this article https://github.com/ddean2009/learn-java-collections

Welcome to my Public Number: program stuff, more exciting waiting for you! More please visit www.flydean.com

Keywords: Programming github Java

Added by fubowl on Sun, 03 May 2020 05:49:24 +0300