Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8247402

Documentation for Map::compute contains confusing implementation requirements

XMLWordPrintable

      A DESCRIPTION OF THE PROBLEM :
      The implementation requirements for Map::compute reads:

       V oldValue = map.get(key);
       V newValue = remappingFunction.apply(key, oldValue);
       if (oldValue != null ) {
          if (newValue != null)
             map.put(key, newValue);
          else
             map.remove(key);
       } else {
          if (newValue != null)
             map.put(key, newValue);
          else
             return null;
       }

      However, it's weird that the code only returns when (oldValue == null && newValue == null). The problem is that the code doesn't return anything otherwise. I think the better way is:

       V oldValue = map.get(key);
       V newValue = remappingFunction.apply(key, oldValue);
       if (oldValue != null ) {
          if (newValue != null)
             map.put(key, newValue);
          else
             map.remove(key);
       } else {
          if (newValue != null)
             map.put(key, newValue);
       }
       return newValue;

      PS. This issue meant to target the documentation of the core libraries, but I couldn't find it in the "Subcomponent" list. So I chose "guides: Documentation related to Guides" instead.


      FREQUENCY : always


            martin Martin Buchholz
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved: