0

I am trying to construct a dictionary cache which contains the weakreference of the passed-in key as the index,instead of the original key.
The source is below - and the line where it fails compilations is indicated.
I need help on the proper way to resolve this error.

public class ProxyWeakReference extends WeakReference{

  public ProxyWeakReference(Object o, ReferenceQueue q)
  {
    super(o,q);
    ........
  }
}

public class SafeQueueMap<K,V>{
    ConcurrentMap<WeakReference<K>,V> d=new ConcurrentHashMap<ProxyWeakReference<K>,V>();
    ReferenceQueue refQueue=new ReferenceQueue();

    public void put(K k ,V v){
        WeakReference<K> r=new ProxyWeakReference(k,refQueue);<==FAILS ON GENERIC PARAMETER
        d.put(r,v);

    }
        ......
        ......
}
IUnknown
  • 7,639
  • 12
  • 43
  • 65
  • possible duplicate of [Java how to: Generic Array creation](http://stackoverflow.com/questions/529085/java-how-to-generic-array-creation) – IUnknown Aug 01 '13 at 06:05

2 Answers2

0

I can't get compiling instruction new ConcurrentHashMap<ProxyWeakReference<K>,V>();! Why you don't use full generics for ProxyWeakReference instead of a messy mix? You can resolve all errors and write a "correct" code.

public class ProxyWeakReference<K> extends WeakReference<K>{
    public ProxyWeakReference(K o, ReferenceQueue<K> q)
    {
        super(o,q);
    }
}

public class SafeQueueMap<K,V>{
    ConcurrentMap<WeakReference<K>,V> d=new ConcurrentHashMap<WeakReference<K>, V>();
    ReferenceQueue<K> refQueue=new ReferenceQueue<K>();

    public void put(K k ,V v){
        WeakReference<K> r=new ProxyWeakReference<K>(k,refQueue);
        d.put((ProxyWeakReference<K>) r,v);
    }
}

or you can use ProxyWeakReference instead of WeakReference in d definition and instantiation.

Luca Basso Ricci
  • 16,612
  • 2
  • 40
  • 61
0

This solved the issue:

public class ProxyWeakReference<K> extends WeakReference{

     int hashdata;

      public  ProxyWeakReference(K referent, ReferenceQueue<? super K> q) {
            super(referent, q);
            hashdata = referent.hashCode();
      }
IUnknown
  • 7,639
  • 12
  • 43
  • 65