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

Collections.singletonMap throw exception from computeIfAbsent

XMLWordPrintable

      A DESCRIPTION OF THE REQUEST :
      SingletonMap have single mapping and should check for existing key before throw exception:

        @Override
        public V computeIfAbsent(K key,
                Function<? super K, ? extends V> mappingFunction) {
            if (eq(key, k))
                return v;
            throw new UnsupportedOperationException();
        }

      Default method from java.util.Map works as expected except mapping function was called.

      JUSTIFICATION :
      Changes in underlying framework which starts using computeIfAbsent cause code changes to avoid SingletonMap

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      method return value
      ACTUAL -
      java.lang.UnsupportedOperationException thrown

      ---------- BEGIN SOURCE ----------
      java.util.Collections.singletonMap("k", "v").computeIfAbsent("k", k -> "v" );
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      use another Map implementation

            smarks Stuart Marks
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: