从用户的角度来看,纯粹的功能数据结构不能改变。因此,如果我从哈希映射中获得一个键,等待,然后再次获得相同的键,我必须获得相同的值。我可以抓住钥匙,所以它们不会消失。
它可以工作的唯一方法是,如果 API 为我提供了下一代,并且在释放对容器过去版本的所有引用之前不收集值。预计数据结构的用户会定期要求新一代发布弱持有的价值。
编辑(基于评论):我理解你想要的行为,但你不能用释放对象的地图通过这个测试:
FunctionalWeakHashMap map = new FunctionalWeakHashMap();
{ // make scope to make o have no references
Object o = new SomeObject();
map["key"] = o;
} // at this point I lose all references to o, and the reference is weak
// wait as much time as you think it takes for that weak reference to collect,
// force it, etc
Assert.isNotNull(map["key"]); // this must be true or map is not persistent
我建议这个测试可以通过
FunctionalWeakHashMap map = new FunctionalWeakHashMap();
{ // make scope to make o have no references
Object o = new SomeObject();
map["key"] = o;
} // at this point I lose all references to o, and the reference is weak in the map
// wait as much time as you think it takes for that weak reference to collect,
// force it, etc
map = map.nextGen();
Assert.isNull(map["key"]);