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

Provide specialized implementation for default methods putIfAbsent, computeIfAbsent, computeIfPresent, compute, merge in TreeMap

    Details

    • Type: CSR
    • Status: Closed
    • Priority: P4
    • Resolution: Approved
    • Fix Version/s: 15
    • Component/s: core-libs
    • Labels:
      None
    • Subcomponent:
    • Compatibility Kind:
      behavioral
    • Compatibility Risk:
      low
    • Compatibility Risk Description:
      Hide
      Two behavior changes are visible:

      1. After this change, the function supplied to the `computeIfAbsent`, `computeIfPresent`, `compute`, and `merge` methods can no longer modify the same `TreeMap`. A `ConcurrentModificationException` will be thrown in this case. (This is similar to `HashMap`'s behavior.)
      2. Subclasses of `TreeMap` that override `get`, `put`, or `remove` might be relying on the fact that the five overridden methods previously had called them. This no longer occurs. As a result, the behavior of such subclass may change.
      Show
      Two behavior changes are visible: 1. After this change, the function supplied to the `computeIfAbsent`, `computeIfPresent`, `compute`, and `merge` methods can no longer modify the same `TreeMap`. A `ConcurrentModificationException` will be thrown in this case. (This is similar to `HashMap`'s behavior.) 2. Subclasses of `TreeMap` that override `get`, `put`, or `remove` might be relying on the fact that the five overridden methods previously had called them. This no longer occurs. As a result, the behavior of such subclass may change.
    • Interface Kind:
      Java API
    • Scope:
      SE

      Description

      Summary

      Specialized implementations of the TreeMap methods putIfAbsent, computeIfAbsent, computeIfPresent, compute, and merge are introduced.

      Problem

      Default implementations of the putIfAbsent, computeIfAbsent, computeIfPresent, compute, and merge methods provided by AbstractMap delegate to the get, put, and remove methods, which may require traversing the tree twice. An optimized implementation can traverse the tree only once. This can provide a noticeable speedup, especially if the comparison method is expensive.

      Solution

      Provide optimized implementations for these methods, similar to HashMap.

      Specification

      • As TreeMap now overrides the methods putIfAbsent, computeIfAbsent, computeIfPresent, compute, and merge, the implSpec section specified for the corresponding default methods in the Map interface is no longer relevant to TreeMap methods.

      • The specification for the TreeMap method computeIfAbsent now additionally covers the concurrent update policy, as follows:

           * <p>This method will, on a best-effort basis, throw a
           * {@link ConcurrentModificationException} if it is detected that the
           * mapping function modifies this map during computation.
           *
           * @throws ConcurrentModificationException if it is detected that the
           * mapping function modified this map
      • The specifications for the TreeMap methods computeIfPresent, compute, and merge now additionally cover the concurrent update policy, as follows:
           * <p>This method will, on a best-effort basis, throw a
           * {@link ConcurrentModificationException} if it is detected that the
           * remapping function modifies this map during computation.
           *
           * @throws ConcurrentModificationException if it is detected that the
           * remapping function modified this map

      These additions are exactly copied from the specification of the corresponding HashMap methods.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                tvaleev Tagir Valeev
                Reporter:
                skuksenko Sergey Kuksenko
                Reviewed By:
                Doug Lea, Martin Buchholz, Stuart Marks
              • Votes:
                0 Vote for this issue
                Watchers:
                5 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: