[introduction to Java] day34 detailed explanation of Java container class (XV) detailed explanation of weakhashmap

The detailed source code series are analyzed based on jdk8

explain

At the end of the Java container explanation series, we introduce a relatively special member: weakhashmap. From its name, we can see that it is a map. Its use is no different from that of HashMap, so we won't introduce too much here. You can look at the content in the previous HashMap. This article mainly introduces the differences between it and HashMap.

The special feature of weakhashmap is that the entries in the weakhashmap may be automatically deleted by the garbage collector, that is, even if you do not call the remove () or clear () method, its entries may gradually decrease. Therefore, multiple calls to methods such as isempty, containskey, size, etc. may return different results.

Next, I hope to read with these questions:

1. Why are entries in weakhashmap automatically recycled.

2. What is the difference between weakhashmap and HashMap.

3. What are the reference scenarios of weakhashmap.

Exploring the secrets of weakhashmap

It can be seen from the description that the special feature of weakhashmap is that its entries are different. The entries in it will be automatically recycled by the garbage collector. The problem is, why will they be automatically recycled? The entry in the HashMap will not be automatically recycled unless it is removed from the map.

In fact, the secret lies in weak references. The keys in weakhashmap are indirectly saved in weak references, so when the keys are not used anymore, they may be recycled during GC.

Only the key object is saved by weak reference, and the value object is actually maintained by ordinary strong reference. Therefore, it should be ensured that value will not directly or indirectly maintain the strong reference of its corresponding key, because this will prevent the key from being recycled.

If you are not familiar with reference types, you can read this article first.

Let's see how to implement this feature from the perspective of source code.

Inheritance structure

Weakhashmap does not inherit from HashMap, but from abstractmap, which is similar to the inheritance structure of HashMap.

storage structure

The data structure in weakhashmap is in the form of array + linked list, which is also consistent with HashMap, but the difference is that in jdk8, when more key conflicts occur, the HashMap will be changed from linked list to red black tree, while weakhashmap always uses linked list for storage.

Member variable

It is almost the same as the member variable of HashMap. There is an additional ReferenceQueue to store the weak reference objects that have been recycled. If you want to know how ReferenceQueue works, you can refer to this article.

Constructor

There are also four constructors in weakhashmap:

You can see that the last three are the first constructor called. Let's take a look at the content of the first constructor:

Take another look at the newtable function.

In fact, this is just a simple creation of an entry array.

Entry parsing

Next, let's look at the core role in weakhashmap - entry. As you can see above, the table in weakhashmap is an entry array:

Let's see what entry looks like:

Entry inherits from WeakReference. The inheritance diagram is as follows:

Let's take a look at the contents in the entry:

Careful, you may find, hey? Where is the key? There is no key in the member variable. Don't worry. Look at the constructor and you can see that it calls the constructor of the parent class.

The constructor of the WeakReference called here passes the key into the reference and saves it in the referent member variable. If you are not familiar with reference and WeakReference, you can refer to this article and this article.

Look at other methods:

Here, let's talk about the getKey method, which calls weakhashmap Unmasknull. This method is called because the special processing of null key in weakhashmap will point it to a special internal variable:

The two corresponding methods are:

Therefore, the biggest difference between entries in other weakhashmaps is that they inherit from the WeakReference and save the key in the WeakReference. It can be said that most of the features of weakhashmap are attributed to WeakReference.

common method

The main methods are:

Here, select the three most commonly used methods for analysis:

Put method

There are many methods involved here. Don't panic, one by one.

Let's first look at the hash method:

The hash method hashes the hashcode of the key twice, mainly to expand the influence of the low order. Because the size of the entry array is a power of 2, it is masked when searching. If the secondary hash is not performed, the low order will have no impact on the index. If it is not clear, it doesn't matter. It will be explained in the get method later.

As for why we choose 20, 12, 7 and 4. Emmm, the effect is probably very good (serious nonsense. If you are interested, you can study it by yourself).

Take another look at the indexfor function. Here is to perform a bit and operation with hashcode after subtracting the array length by 1. Because the length must be a power of 2, it becomes a mask after subtracting 1. Then, the result of hashcode mod length can be obtained directly by and operation, but the operation efficiency will be higher.

Let's look at the gettable method:

Therefore, every time gettable is called, the entries in the table whose keys have been recycled will be removed.

Resize method:

Get method

An EQ method is called when looking for elements:

Remove method

Use chestnuts

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>