如何克隆/复制WeakHashMap? (深拷贝)
我有一个 HashMap,我使用克隆函数进行深复制。但我将其切换为 WeakHashMap 来尝试一些内存管理。
我仍然想做这个克隆函数,但它不是 WeakHashMap 中方法的一部分。如何有效地将 WeakHashMap 克隆到另一个 WeakHashMap 中?我想我可以做一个循环,添加密钥并检查现有密钥等,但我不确定这是否是最好的方法。
I had a HashMap and I used the clone function to make a deep copy. But I switched this to a WeakHashMap to try out some memory management.
I still want to do this clone function but it isn't part of the methods in WeakHashMap. How do I efficiently clone a WeakHashMap into another one? I suppose I could do a loop, adding keys and checking for existing keys etc, but I'm not sure if that's the best way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您必须复制映射中包含的所有元素,因此需要
for
循环才能顺序访问所有键和值对。As you have to copy all the elements contained in your map, a
for
loop is required in order to sequentially visit all key and value pairs.