国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Java編程WeakHashMap實例解析

Java編程WeakHashMap實例解析

2021-03-31 13:56anialy Java教程

這篇文章主要介紹了Java編程WeakHashMap實例解析,分享了相關代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下

簡述:

《Thinking in Java》第4版 P519 頁 WeakHashMap一章讀書筆記

WeakHashMap 用來保存WeakReference,這一結構云遜垃圾回收器自動清理鍵和值

在添加鍵和值的操作時,映射會自動使用WeakReference包裝它們,

見jdk源代碼,

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public V put(K key, V value) {
    Object k = maskNull(key);
    int h = hash(k);
    Entry<K,V>[] tab = getTable();
    int i = indexFor(h, tab.length);
    for (Entry<K,V> e = tab[i]; e != null; e = e.next) {
        if (h == e.hash && eq(k, e.get())) {
            V oldValue = e.value;
            if (value != oldValue)
                    e.value = value;
            return oldValue;
        }
    }
    modCount++;
    Entry<K,V> e = tab[i];
    tab[i] = new Entry<>(k, value, queue, h, e);
    if (++size >= threshold)
        resize(tab.length * 2);
    return null;
}

其中new Entry<>(k, value, queue, h, e)一行使用了ReferenceQueue

?
1
2
3
4
/**
 * Reference queue for cleared WeakEntries
 */
private final ReferenceQueue<Object> queue = new ReferenceQueue<>();

點入new Entry的構造函數,進入super頂層可以看到,

?
1
2
3
4
5
6
7
8
9
10
11
/**
 * Creates a new weak reference that refers to the given object and is
 * registered with the given queue.
 *
 * @param referent object the new weak reference will refer to
 * @param q the queue with which the reference is to be registered,
 *     or <tt>null</tt> if registration is not required
 */
public WeakReference(T referent, ReferenceQueue<? super T> q) {
  super(referent, q);
}

這里new Entry同時也構造出來了一個WeakRefence對象

測試:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.anialy.test.data_structure.map;
import java.util.Iterator;
import java.util.WeakHashMap;
public class WeakHashMapTest {
    public static void main(String[] args) {
        WeakHashMap wmap = new WeakHashMap<String, Object>();
        final int SIZE = 10;
        String[] str = new String[SIZE];
        for (int i=0; i<SIZE; i++){
            String key = Integer.toString(i);
            String value = Integer.toString(i);
            // 每隔3個保留一個引用
            if(i % 3 == 0)
                    str[i] = key;
            wmap.put(key, value);
        }
        System.gc();
        Iterator iter = wmap.keySet().iterator();
        while(iter.hasNext()){
            System.out.println(wmap.get(iter.next()));
        }
    }
}

可以預料到,部分由于String[] 保留了弱引用,所以輸出都是間隔3的

Java編程WeakHashMap實例解析

總結

以上就是本文關于Java編程WeakHashMap實例解析的全部內容,希望對大家有所幫助。感興趣的朋友可以繼續參閱本站其他相關專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!

原文鏈接:http://blog.csdn.net/anialy/article/details/39273345

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 一区在线视频观看 | 色综合色综合网色综合 | 亚洲一区二区三区 | 亚洲欧美一级 | 久久妇女 | 污污视频网站免费 | www.夜夜骑| 成人精品99| 中文精品在线 | 亚洲精品一区二三区不卡 | 国产精品久久久久久久久久免费 | 亚洲日本国产 | 亚洲精品电影在线观看 | 欧美日韩成人一区 | 国产激情不卡 | 日韩精品 | www.久久久久久久 | 欧美视频一区二区 | 永久免费看黄网站 | 在线观看国产一区 | 精品久久久精品 | 午夜网| 日韩在线免费观看视频 | 久久草在线视频 | 国产91久久久久蜜臀青青天草二 | 黄色片视频在线播放 | 自拍亚洲欧美 | 欧美与黑人午夜性猛交久久久 | 这里只有精品视频 | 精品国产乱码久久久久久牛牛 | 日本一区二区免费在线观看 | www,四虎| 亚洲国产高清在线 | 在线四区 | 精品一二区 | 99精品国产高清在线观看 | 国产尤物 | 中国一级毛片 | 亚洲精品免费视频 | 色婷网| 国产婷婷精品av在线 |