-1

I have a linked hash map which stores random 6 char string as a key and 30 char string as values. When I call replace method, it is supposed to replace value for given key and return existing value associated with given key.
Code

    Map cache = new LinkedHashMap<String, String>();

protected boolean registerCache(String key, String val) {
    System.out.println("Registering key "+ key +" associated with  : "+val);
    String result = cache.put(key, val);
    System.out.println("Replacement result "+result);
    return result == null;
}

protected synchronized boolean updateCache(String key, String val) {
    System.out.println("map before replace  : "+cache.toString());
    String replaced = cache.replace(key, val);
    System.out.println("replacing "+replaced+" with "+val);
    return  replaced != null;
}

Register cache stores key value for first time and then update method is supposed to replace value for registered key. But once in 4 times, it fails to replace. It behaves as key was never registered. Here is output:

Registering key \b?}`& associated with : Vtw7vd3Mtk9DEImmZAxfazKrckVpt4
Replacement result null
 map before replace: {
        d\ZDO<=9pw7cEjdnvWhpbxar564kiSkVpt4Z1,
        pHQ)j\=9pw7cEjdnvWhpbxar564kiSkVpt4Z1,
        0''nEY=KxE7vdInrD2goNOU5LdMFdEMgsCh-1,
        C\Gude=KxE7vdInrD2goNOU5LdMFdEMgsCh-1,
        \b?}`&=Vtw7vd3Mtk9DEImmZAxfazKrckVpt4}
replacing null with KxE7vdInrD2goNOU5LdMFdEMgsCh-1

Please suggest if I am doing something wrong. I suspect the key generated should not be random char string.

kAmol
  • 1,297
  • 1
  • 18
  • 29
  • Wht values are you passing to `updateCache`? Please provide a minimal reproducible example https://stackoverflow.com/help/minimal-reproducible-example – MikeFHay Mar 16 '21 at 16:29
  • Is it **every** 4 times that this happens? Also, it seems the problem is with the value you're passing, not with the key. The variable `String replaced` is not the key, but the value you replaced – Soutzikevich Mar 16 '21 at 16:31
  • random 6 char string as a key and 30 char string as values. The key is properly being passed and that part is being handled in another class, so I have mentioned only problem code. – kAmol Mar 16 '21 at 16:33
  • @Soutzikevich Its not fixed for once in 4 times, its random but as frequent as once in 4 times. It's just my observation. – kAmol Mar 16 '21 at 16:34
  • 1
    the methods `Map.put(K, V)` and `Map.replace(K, V)` both return V. So you are only printing the value, never the key. Was this your intention? – Soutzikevich Mar 16 '21 at 16:35
  • @kAmol It would really help if you posted the part of your code where you are actually registering and updating – Soutzikevich Mar 16 '21 at 16:36
  • @Soutzikevich yes, I understand `replaced` is value and not key, but as I see in result, value is associated with `\b?}`&` before calling replace but still replace method returns `null` instead it is null. – kAmol Mar 16 '21 at 16:36
  • @kAmol Is there a reason you're using a linkedHashMap? Do you need your items to be ordered? – Soutzikevich Mar 16 '21 at 16:38
  • @Soutzikevich yes, in order to delete older entries registered. I am limiting cache entries, for test its 10 now. – kAmol Mar 16 '21 at 16:42
  • @kAmol At what point do you randomly generate your keys?Are you sure that you passed a key that already exists into your methods? edit: To me it honestly looks like you are mixing up what key and value stand for in a map and try to use it the wrong way around (mapping a key to a value, which you can't do...maps are for mapping values to keys) – OH GOD SPIDERS Mar 16 '21 at 16:47
  • @Soutzikevich : yes, I am just printing values and not keys. as I am printing entire map before calling replace and tried with debug and can confirm the keys are properly being passed to update related value. – kAmol Mar 16 '21 at 16:54
  • 1
    You could add `System.out.println("key and value before replace : "+key+" - "+cache.get(key));` to verify that key and old value are as expected. – Thomas Kläger Mar 16 '21 at 19:54

1 Answers1

0

I figured out reason. As suggested by Thomas in comment above, I added more sysout for Keys against which value needs to be replaced.
I was using RandomStringUtils.randomAscii(6) (from commons library) for generating key. The key generated had some chars which were printed with spaces and hence the key was not properly found to replace value against it.
When I replaced RandomStringUtils.randomAscii(6) with RandomStringUtils.randomAlphanumeric(8), it's behaving as expected.

kAmol
  • 1,297
  • 1
  • 18
  • 29