WeakHashMap

 java.util.WeakHashMap<KV>


下面是JDK文档中的解释。这个类简单的说就是它是Map的一个实现,特别的地方是,一个Entry是够存在,跟这个Map半毛钱关系没有。及时这个Map中引用了一个键值对,过期后,仍然会被垃圾回收期回收。对于一些缓存是很适用的。

简单的讲,由于其短暂性,很多情形会与常用的Map产生差异,需要谨慎处理。

其Entry的实现为:

private static class Entry<K,V> extends WeakReference<Object> implements Map.Entry<K,V>

Hash table based implementation ofthe Map interface, with weak keys. An entry ina WeakHashMap will automatically be removed when its key is no longerin ordinary use. More precisely, the presence of a mapping for a given key willnot prevent the key from being discarded by the garbage collector, that is,made finalizable, finalized, and then reclaimed. When a key has been discardedits entry is effectively removed from the map, so this class behaves somewhatdifferently from other Map implementations.

Both null values and the null key are supported. This class hasperformance characteristics similar to those of the HashMap class,and has the same efficiency parameters ofinitial capacity and loadfactor.

Like most collection classes, this class is not synchronized. Asynchronized WeakHashMap may be constructed using the Collections.synchronizedMap method.

This class is intended primarily for use with key objectswhose equals methods test for object identity usingthe == operator. Once such a key is discarded it can never berecreated, so it is impossible to do a lookup of that key in a WeakHashMap atsome later time and be surprised that its entry has been removed. This classwill work perfectly well with key objects whose equals methods arenot based upon object identity, such as String instances. With suchrecreatable key objects, however, the automatic removalof WeakHashMap entries whose keys have been discarded may prove to beconfusing.

The behavior of the WeakHashMap class depends in partupon the actions of the garbage collector, so several familiar (though notrequired) Map invariants do not hold for this class. Because thegarbage collector may discard keys at any time, a WeakHashMap maybehave as though an unknown thread is silently removing entries. In particular,even if you synchronize on a WeakHashMap instance and invoke none ofits mutator methods, it is possible for the size method to returnsmaller values over time, for theisEmpty method toreturn false and then true, for the containsKey methodto return true and later false for a given key, forthe get method to return a value for a given key but laterreturn null, for the put method to return null andthe remove method to return false for a key that previouslyappeared to be in the map, and for successive examinations of the key set, thevalue collection, and the entry set to yield successively smaller numbers ofelements.

Each key object in a WeakHashMap is stored indirectly asthe referent of a weak reference. Therefore a key will automatically be removedonly after the weak references to it, both inside and outside of the map, havebeen cleared by the garbage collector.

Implementation note: The value objects ina WeakHashMap are held by ordinary strong references. Thus careshould be taken to ensure that value objects do not strongly refer to their ownkeys, either directly or indirectly, since that will prevent the keys frombeing discarded. Note that a value object may refer indirectly to its key viatheWeakHashMap itself; that is, a value object may strongly refer to someother key object whose associated value object, in turn, strongly refers to thekey of the first value object. If the values in the map do not rely on the mapholding strong references to them, one way to deal with this is to wrap valuesthemselves within WeakReferences before inserting, asin: m.put(key, new WeakReference(value)), and then unwrapping uponeach get.

The iterators returned by the iterator method of thecollections returned by all of this class's "collection view methods"are fail-fast: if the map is structurally modified at any time after theiterator is created, in any way except through the iterator'sown remove method, the iterator will throw a ConcurrentModificationException.Thus, in the face of concurrent modification, the iterator fails quickly andcleanly, rather than risking arbitrary, non-deterministic behavior at anundetermined time in the future.

Note that the fail-fast behavior of an iterator cannot beguaranteed as it is, generally speaking, impossible to make any hard guaranteesin the presence of unsynchronized concurrent modification. Fail-fast iteratorsthrow ConcurrentModificationException on a best-effort basis.Therefore, it would be wrong to write a program that depended on this exceptionfor its correctness: the fail-fast behavior of iterators should be usedonly to detect bugs.

This class is a member of the Java Collections Framework.

Parameters:

<K> thetype of keys maintained by this map

<V> thetype of mapped values

Since:

1.2

Author:

DougLea

JoshBloch

MarkReinhold

See Also:

java.util.HashMap

java.lang.ref.WeakReference


标题基于Python的自主学习系统后端设计与实现AI更换标题第1章引言介绍自主学习系统的研究背景、意义、现状以及本文的研究方法和创新点。1.1研究背景与意义阐述自主学习系统在教育技术领域的重要性和应用价值。1.2国内外研究现状分析国内外在自主学习系统后端技术方面的研究进展。1.3研究方法与创新点概述本文采用Python技术栈的设计方法和系统创新点。第2章相关理论与技术总结自主学习系统后端开发的相关理论和技术基础。2.1自主学习系统理论阐述自主学习系统的定义、特征和理论基础。2.2Python后端技术栈介绍DjangoFlask等Python后端框架及其适用场景。2.3数据库技术讨论关系型和非关系型数据库在系统中的应用方案。第3章系统设计与实现详细介绍自主学习系统后端的设计方案和实现过程。3.1系统架构设计提出基于微服务的系统架构设计方案。3.2核心模块设计详细说明用户管理、学习资源管理、进度跟踪等核心模块设计。3.3关键技术实现阐述个性化推荐算法、学习行为分析等关键技术的实现。第4章系统测试与评估对系统进行功能测试和性能评估。4.1测试环境与方法介绍测试环境配置和采用的测试方法。4.2功能测试结果展示各功能模块的测试结果和问题修复情况。4.3性能评估分析分析系统在高并发等场景下的性能表现。第5章结论与展望总结研究成果并提出未来改进方向。5.1研究结论概括系统设计的主要成果和技术创新。5.2未来展望指出系统局限性并提出后续优化方向。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值