2

I want to store some data in WeakHashMap, but there is a problem. Say we have a code:

public class WeakMapTest {
    public static void main(String[] args) {
        WeakHashMap<Object, Object> map = new WeakHashMap<Object, Object>();
        map.put(null, 0);
        map.put(new Integer(1), null);
        map.put(new Integer(2), 2);
        System.out.println(map.get(new Integer(2)));

        System.gc(); //assume this call made implicitly by JVM
        if (map.containsKey(new Integer(2))) {
            System.out.println(map.get(new Integer(2)));
        } else {
            System.out.println("Key is deleted");
        }
    }
}

Output will be

2
Key is deleted

which is logical.

But there is another case:

    if (map.containsKey(new Integer(2))) {
        System.gc(); //assume this call made implicitly by JVM
        System.out.println(map.get(new Integer(2)));
    } else {
        System.out.println("Key is deleted");
    }

and the results is not as good:

2
null

How can I avoid such misleading results? Bare in mind that values and keys can be null.

DeGriz
  • 318
  • 3
  • 15

1 Answers1

1

Find my the solution while I was writing this question, so just decided to share.

Object value = map.get(new Integer(2));
if (map.containsKey(new Integer(2))) {
    System.gc(); // can happen here
    System.out.println(value);
} else {
    System.out.println("Key is deleted");
}

I have to get value first and only then check for key existence. That way I am protected against incorrect result. Current result is:

2
2

that is correct, at least for my case.

DeGriz
  • 318
  • 3
  • 15
  • 1
    Your solution isn't exactly optimal. You should do `Object value = map.get(new Integer(2)); if(value != null) System.out.println(value);`. Besides, there are no "incorrect results". You're using `WeakHashMap` wrong if you don't want them to be collected. – Kayaman May 12 '14 at 11:51
  • I explicitly said that values can be null, so your optimisation will not work for my case. I do want them collected (to prevent OutOfMemory in some cases), but when I use 'get' function I don't know if null is the original object that I put or if it was garbage collected. – DeGriz May 12 '14 at 11:53
  • @Kayaman Comparing with `null` is not a good idea if `null` values are allowed. – assylias May 12 '14 at 12:05
  • 1
    @assylias Ah, didn't notice the example put a null value in there. I understood that this was to be used as a sort of cache (although I'd advise against rolling your own cache like this). – Kayaman May 12 '14 at 12:09
  • It's somewhat difficult to see why you would be weakly caching a null value. – user207421 May 12 '14 at 12:33
  • In project that I am working on it makes sense although I do understand your skepticism. I just had to modify legacy code without breaking it. – DeGriz May 12 '14 at 13:51