本文介绍了如何从列表中删除weakReference?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个java中对象的weakReferences列表.我该如何写一个方法来获取真实对象实例并从列表中删除它的弱引用?

I've got a list of weakReferences to objects in java.How do i write a method that gets the real object instance and removes it's weak reference from this list?

谢谢.

推荐答案

您的意思尚不完全清楚,但我认为您可能想要:

It's not entirely clear what you mean, but I think you may want:

public static <T> void removeReference(List<WeakReference<T>> list,
                                       T reference)
{
    for (Iterator<WeakReference<T>> iterator = list.iterator();
         iterator.hasNext(); )
    {
        WeakReference<T> weakRef = iterator.next();
        if (weakRef.get() == reference)
        {
            iterator.remove();
        }
    }
}

这篇关于如何从列表中删除weakReference?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 12:50