s*i 发帖数: 388 | |
B*****g 发帖数: 34098 | 2 新手表示看不懂,这个不是gc会处理吗?
【在 s*i 的大作中提到】 : 【 以下文字转载自 JobHunting 讨论区 】 : 发信人: sci (ence), 信区: JobHunting : 标 题: java知道一个reference怎么删掉它指向的内存空间? : 发信站: BBS 未名空间站 (Thu Jun 9 11:51:36 2011, 美东) : java。 : 维护了一个 HashMap 和 List, A是我自己定义的一个class. : 每次new一个A之后,先add到 list里面,然后再 加到map里面。 : A a = new A(); : list.add(a); : map.put(key, a);
|
s*i 发帖数: 388 | 3 可是我怀疑gc会不会清理掉那个需要被remove的object,因为他还是被referenced了,
reference count != 0, 所以gc应该不会清理它。
【在 B*****g 的大作中提到】 : 新手表示看不懂,这个不是gc会处理吗?
|
s*i 发帖数: 388 | 4 可是我怀疑gc会不会清理掉那个需要被remove的object,因为他还是被referenced了,
reference count != 0, 所以gc应该不会清理它。
【在 B*****g 的大作中提到】 : 新手表示看不懂,这个不是gc会处理吗?
|
g*****g 发帖数: 34805 | 5 A value = map.remove(key)
list.remove(value)
【在 s*i 的大作中提到】 : 【 以下文字转载自 JobHunting 讨论区 】 : 发信人: sci (ence), 信区: JobHunting : 标 题: java知道一个reference怎么删掉它指向的内存空间? : 发信站: BBS 未名空间站 (Thu Jun 9 11:51:36 2011, 美东) : java。 : 维护了一个 HashMap 和 List, A是我自己定义的一个class. : 每次new一个A之后,先add到 list里面,然后再 加到map里面。 : A a = new A(); : list.add(a); : map.put(key, a);
|
m****r 发帖数: 6639 | 6 我没有看懂, 这个c怎么就可以更容易解决了?
【在 g*****g 的大作中提到】 : A value = map.remove(key) : list.remove(value)
|
g*****g 发帖数: 34805 | 7 我也不明白
【在 m****r 的大作中提到】 : 我没有看懂, 这个c怎么就可以更容易解决了?
|
B*****g 发帖数: 34098 | 8 学习
【在 g*****g 的大作中提到】 : A value = map.remove(key) : list.remove(value)
|
z***e 发帖数: 5393 | 9 为什么reference count!=0?你只有两个地方reference它,一个在map,一个在list,
两边都
remove的话,reference count == 0.
【在 s*i 的大作中提到】 : 可是我怀疑gc会不会清理掉那个需要被remove的object,因为他还是被referenced了, : reference count != 0, 所以gc应该不会清理它。
|
s*i 发帖数: 388 | 10 how does this list.remove(value) work in java?
will it do a linear scan in the list ? im looking for a more efficient way.
【在 g*****g 的大作中提到】 : A value = map.remove(key) : list.remove(value)
|
|
|
r*****l 发帖数: 2859 | 11 List is just an interface. The implementation depends the classes that
implements the interface.
Are you sure that you want List, not Set? Remove operation in a List is not
that efficient in Java, at least for ArrayList and LinkedList.
【在 s*i 的大作中提到】 : how does this list.remove(value) work in java? : will it do a linear scan in the list ? im looking for a more efficient way.
|
r***y 发帖数: 4379 | 12 俺没记错的话
A *a;
a = new A;
/* blah blah */
delete a;
a = 0;
然后整个世界就清静了...
【在 m****r 的大作中提到】 : 我没有看懂, 这个c怎么就可以更容易解决了?
|
r***y 发帖数: 4379 | 13 all out-box list implementations in JDK are using sequential search, which
achieve O(N)...
for better performance, you need your own implementation to do indexing,
hashing... whatever
【在 s*i 的大作中提到】 : how does this list.remove(value) work in java? : will it do a linear scan in the list ? im looking for a more efficient way.
|
r***y 发帖数: 4379 | 14 hmmm...
if the scope of "A a" is big, need to null out it too...
【在 g*****g 的大作中提到】 : A value = map.remove(key) : list.remove(value)
|
g*****g 发帖数: 34805 | 15 得,C++里你把a放到一个list,然后不dereference的话,
就是一个悬挂指针。
【在 r***y 的大作中提到】 : 俺没记错的话 : A *a; : a = new A; : /* blah blah */ : delete a; : a = 0; : 然后整个世界就清静了...
|
g*****g 发帖数: 34805 | 16 If you are looking for efficiency, you can use a TreeSet to back
your list. Or if you don't use list at all, why do you need another data
structure, HashMap.values() doesn't work for you?
【在 s*i 的大作中提到】 : how does this list.remove(value) work in java? : will it do a linear scan in the list ? im looking for a more efficient way.
|
r***y 发帖数: 4379 | 17 Ooooooooops!
goodbug is right, thanks for correcting me...
my c++ knowledge has been totally gone... :-P
【在 g*****g 的大作中提到】 : 得,C++里你把a放到一个list,然后不dereference的话, : 就是一个悬挂指针。
|
s******e 发帖数: 493 | 18 list.remove(a) can be a dangerous game if it is called inside a loop.
you may get the famous ConcurrentModificationException.
using iterator. |
x***i 发帖数: 585 | |
z****e 发帖数: 54598 | 20 removeAll()是我的最爱啊
【在 s******e 的大作中提到】 : list.remove(a) can be a dangerous game if it is called inside a loop. : you may get the famous ConcurrentModificationException. : using iterator.
|