从Java集合中删除对象

编程入门 行业动态 更新时间:2024-10-28 20:30:40
本文介绍了从Java集合中删除对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个 HashMap (虽然我猜这个问题适用于其他集合)的对象。从我的理解,当文档谈到删除映射,然后它从哈希表中删除条目,即不一定破坏实际的对象。

I have a HashMap (although I guess this question applies to other collections) of objects. From what I understand, when the documentation talks about removing mappings, then it is removing the entry from the hashtable, i.e. not necessarily destroying the actual object. If the only remaining reference to the object is in this table, then will the object get garbage collected?

如果我做 map.clear(),那么这个对象的剩余引用是在这个表中, 和那些在表中的对象没有被引用到其他地方,他们会收集垃圾吗?

If I do map.clear() and those objects that were in the table are not referenced anywhere else, will they get garbage collected?

什么是最快的方式,从表中删除所有条目,但也会销毁那些对象。

What is the fastest way, to actually remove all entries from the table, but also destroy those objects.

推荐答案

是的,如果集合是这些对象被引用的最后一个位置,收集。没有,你不能强有力地破坏这些对象。垃圾收集器会在感觉到它时处理它们。

Yes, if the collection is the last place these objects are referenced they are eligible for garbage collection after they have been removed from the collection. And no, you can not destroy these objects forcefully. The garbage collector will handle them when it feels like it.

更多推荐

从Java集合中删除对象

本文发布于:2023-08-01 05:22:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1265564.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!