从Java集合中删除对象 [英] Removing objects from Java Collections

查看:317
本文介绍了从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集合中删除对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆