I write a simple class (the merit of StringPools themselves is not my question; I have specific reasons for wanting this class):
import java.lang.ref.WeakReference;
import java.util.WeakHashMap;
public final class StringPool {
private WeakHashMap m_pool = new WeakHashMap();
public String intern(String s) {
WeakReference reference = (WeakReference)m_pool.get(s);
if(reference != null) {
String interned = (String)reference.get();
if(interned != null)
return interned;
}
m_pool.put(s,new WeakReference(s));
return s;
}
};
When I compile it, the compiler notes it uses unchecked or unsafe operations:
warning: [unchecked] unchecked call to WeakReference(T) as a member of the raw type WeakReference
m_pool.put(s,new WeakReference(s));
^ where T is a type-variable:
T extends Object declared in class WeakReference
warning: [unchecked] unchecked call to put(K,V) as a member of the raw type WeakHashMap
m_pool.put(s,new WeakReference(s));
^ where K,V are type-variables:
K extends Object declared in class WeakHashMap
V extends Object declared in class WeakHashMap
2 warnings
How should I fix these warnings so the code compiles clean and is correct?
Add generics:
remove casting from:
and:
to look like:
and
respective.