首页 > 其他分享 >Map---WeakHashMap

Map---WeakHashMap

时间:2023-11-24 20:13:16浏览次数:27  
标签:Map WeakHashMap object value class --- key method

概述

Hash table based implementation of the <tt>Map</tt> interface, with <em>weak keys</em>.
An entry in a <tt>WeakHashMap</tt> will automatically be removed when its key is no longer in ordinary use.
More precisely, the presence of a mapping for a given key will not prevent the key from being discarded by the garbage collector, that is, made finalizable, finalized, and then reclaimed.
When a key has been discarded its entry is effectively removed from the map, so this class behaves somewhat differently from other <tt>Map</tt> implementations.

基于Map的哈希表实现,有weak keys;

当WeakHashMap的key不再被使用时,entry将会被remove;

 

Both null values and the null key are supported.
This class has performance characteristics similar to those of the <tt>HashMap</tt> class, and has the same efficiency parameters of <em>initial capacity</em> and <em>load factor.

支持null key、null value

与HashMap有类似的性能;

 

Like most collection classes, this class is not synchronized.
A synchronized <tt>WeakHashMap</tt> may be constructed using the {@link Collections#synchronizedMap Collections.synchronizedMap} method.

线程非同步

可以使用Collections.synchronizedMap;

 

This class is intended primarily for use with key objects whose <tt>equals</tt> methods test for object identity using the <tt>==</tt> operator.
Once such a key is discarded it can never be recreated, so it is impossible to do a lookup of that key in a <tt>WeakHashMap</tt> at some later time and be surprised that its entry has been removed.
This class will work perfectly well with key objects whose <tt>equals</tt> methods are not based upon object identity, such as <tt>String</tt> instances.
With such recreatable key objects, however, the automatic removal of <tt>WeakHashMap</tt> entries whose keys have been discarded may prove to be confusing.

 

The behavior of the <tt>WeakHashMap</tt> class depends in part upon the actions of the garbage collector, so several familiar (though not required) <tt>Map</tt> invariants do not hold for this class.
Because the garbage collector may discard keys at any time, a <tt>WeakHashMap</tt> may behave as though an unknown thread is silently removing entries.
In particular, even if you synchronize on a <tt>WeakHashMap</tt> instance and invoke none of its mutator methods, it is possible for the <tt>size</tt> method to return smaller values over time, for the <tt>isEmpty</tt> method to return <tt>false</tt> and then <tt>true</tt>, for the <tt>containsKey</tt> method to return <tt>true</tt> and later <tt>false</tt> for a given key, for the <tt>get</tt> method to return a value for a given key but later return <tt>null</tt>, for the <tt>put</tt> method to return <tt>null</tt> and the <tt>remove</tt> method to return <tt>false</tt> for a key that previously appeared to be in the map, and for successive examinations of the key set, the value collection, and the entry set to yield successively smaller numbers of elements.

 

Each key object in a <tt>WeakHashMap</tt> is stored indirectly as the referent of a weak reference.
Therefore a key will automatically be removed only after the weak references to it, both inside and outside of the map, have been cleared by the garbage collector.

WeakHashMap的每个key间接存储为弱引用

 

Implementation note:
The value objects in a <tt>WeakHashMap</tt> are held by ordinary strong references.
Thus care should be taken to ensure that value objects do not strongly refer to their own keys, either directly or indirectly, since that will prevent the keys from being discarded.
Note that a value object may refer indirectly to its key via the <tt>WeakHashMap</tt> itself; that is, a value object may strongly refer to some other key object whose associated value object, in turn, strongly refers to the key of the first value object.
If the values in the map do not rely on the map holding strong references to them, one way to deal with this is to wrap values themselves within <tt>WeakReferences</tt> before inserting, as in: <tt>m.put(key, new WeakReference(value))</tt>, and then unwrapping upon each <tt>get</tt>.

WeakHashMap的value是强引用

可以使用WeakReferences将value进行包装(m.put(key, new WeakReference(value)));

 

The iterators returned by the <tt>iterator</tt> method of the collections returned by all of this class's "collection view methods" are <i>fail-fast</i>: if the map is structurally modified at any time after the iterator is created, in any way except through the iterator's own <tt>remove</tt> method, the iterator will throw a {@link ConcurrentModificationException}.
Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.

iterator是fail-fast

 

标签:Map,WeakHashMap,object,value,class,---,key,method
From: https://www.cnblogs.com/anpeiyong/p/17854637.html

相关文章

  • XRender - 开源之路
    XRender-开源之路思翊呀唱、跳、rap、code XRender 是由阿里飞猪内部孵化出的开源产品,目前在GitHub上有4.2kstar;本篇文章不会对XRender的用法进行赘述;我们的目的在于让更多人了解到XRender在这一年内发生了哪些变化,并让XRender能够帮助更多的前......
  • 2023-2024-1 20211306 密码系统设计与实现课程学习笔记11
    20211306密码系统设计与实现课程学习笔记11任务详情自学教材第13章,提交学习笔记知识点归纳以及自己最有收获的内容,选择至少2个知识点利用chatgpt等工具进行苏格拉底挑战,并提交过程截图,提示过程参考下面内容“我在学***X知识点,请你以苏格拉底的方式对我进行提问,一次一个......
  • hyper-v 增加硬盘并分配空间到指定磁盘
    首先关闭要分配磁盘的虚拟机点击【设置】选择【SCSI控制器】选择驱动器类型为【硬盘驱动器】点击【添加】选择【物理磁盘】,点击【应用】,然后点击【确定】开启【启动】,进入虚拟机内,选择【管理工具】-》【计算机管理】-》【磁盘管理】,在刚刚挂载的磁盘上右击......
  • 18-基础SQL-函数-流程函数
    什么是函数:是指一段可以直接被另一段程序调用的程序或代码MySQL的函数主要包括:字符串函数、数值函数、日期函数、流程函数。流程函数流程函数也是很常用的一类函数,可以在SQL语句中实现条件筛选,从而提高语句的效率。IF(value,t,f)演示:当条件为true时,返回"OK",否则返回"ERROR"......
  • url-parse
    url-parse1.5.10 • Public • Published 2yearsago ReadmeCode Beta2Dependencies2,731Dependents51Versionsurl-parseurl-parse wascreatedin2014whentheWHATWGURLAPIwasnotavailableinNode.jsandthe URL interfacewassupport......
  • 【转】浅谈调试--赖特
    浅谈调试——赖特何为调试及为什么要调试调试是程序运行结果与期望结果不统一时,在手动计算模拟的前提下编译程序,对比不同,修正语法错误和逻辑错误的过程。这是保证计算机信息系统正确性的必不可少的步骤。运行代码只能得到两种结果:Ac或Wa。但是程序很笨,不能直接告诉你错哪里......
  • Adaptive Sparse Pairwise Loss for Object Re-Identification
    https://blog.csdn.net/amusi1994/article/details/130037400tripletloss中需要计算每个样本之间的距离,从而计算出loss,作者认为同一类的某些样本可能存在有害的信息,所以不应该将所有样本都用于计算loss。作者提出的SPloss中只计算挑选出来的样本的距离,从而得到loss。我们提出......
  • 无涯教程-Tk - Line部件函数
    线小部件用于在画布中绘制线。行小部件的语法如下所示-canvasNamecreatelinex1y1x2y2...xnynoptionsx1y1,x2y2...xnyn用于确定线段的终点。Line-参数下表在下面列出了可用于行小部件的选项-Sr.No.Syntax&Remark1-arrowend确定线是否在末端具有箭头......
  • 【2023-11-24】这是愿景
    20:00理解得越深,我就越是坚信,活在这个世界上的人都能够不停地改变自己。                                                 ——卡伦·霍妮我昨天跟那位老同事朋友强调......
  • 好用的IDEA插件——Apipost-Helper-2.0
    今天给大家推荐IDEA插件:ApipostHelper,比市面上很多产品好用并且完全免费!先说一下ApipostHelper的安装和使用安装在IDEA编辑器插件中心输入Apipost搜索安装:Apipost-Helper-2.0配置方法:使用ApipostIDEA插件前需要在IDEA设置中进行配置:云端域名默认为:https://sync-project-ide.a......