Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove a weakReference from a list?

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?

thanks.

like image 580
Adibe7 Avatar asked Jun 09 '11 16:06

Adibe7


People also ask

What is the use of WeakReference?

Weak reference objects, which do not prevent their referents from being made finalizable, finalized, and then reclaimed. Weak references are most often used to implement canonicalizing mappings. Suppose that the garbage collector determines at a certain point in time that an object is weakly reachable.

What is a weakref object in Python?

Weak Reference Objects Weak reference objects have no methods and no attributes besides ref.__callback__ . A weak reference object allows the referent to be obtained, if it still exists, by calling it: >>> >>> import weakref >>> class Object: ...

What is WeakReference C#?

A weak reference permits the garbage collector to collect the object while still allowing the application to access the object. A weak reference is valid only during the indeterminate amount of time until the object is collected when no strong references exist.

How WeakReference works Java?

A weakly referenced object is cleared by the Garbage Collector when it's weakly reachable. Weak reachability means that an object has neither strong nor soft references pointing to it. The object can be reached only by traversing a weak reference.


2 Answers

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();
        }
    }
}
like image 153
Jon Skeet Avatar answered Oct 05 '22 01:10

Jon Skeet


Have a look at the Javadocs for WeakReference. Two important things to note: 1. it is protected, so you can extend it, and 2. it does not override Object.equals()

So, two approaches to do what you want:

First, the simple way, use what @Jon Skeet said.

Second, more elegant way. Note: this only works if you are the only one adding to the list too:

public class HardReference<T> {
  private final T _object;

  public HardReference(T object) {
    assert object != null;
    _object = object;
  }

  public T get() { return _object; }

  public int hashCode() { return _object.hashCode(); }

  public boolean equals(Object other) {
    if (other instanceof HardReference) {
      return get() == ((HardReference) other).get();
    }
    if (other instanceof Reference) {
      return get() == ((Reference) other).get();
    }
    return get() == other;
  }
}

class WeakRefWithEquals<T> extends WeakReference<T> {

  WeakRefWithEquals(T object) { super(object); }

  public boolean equals(Object other) {
    if (other instanceof HardReference) {
      return get() == ((HardReference) other).get();
    }
    if (other instanceof Reference) {
      return get() == ((Reference) other).get();
    }
    return get() == other;
  }
}

Once you have these utility classes, you can wrap objects stored in Lists, Maps etc with whatever reference subclass -- like the WeakRefWithEquals example above. When you are looking for an element, you need to wrap it HardReference, just in case the collection implementation does

param.equals(element)

instead of

element.equals(param)
like image 41
Dilum Ranatunga Avatar answered Oct 05 '22 03:10

Dilum Ranatunga