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

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

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P4 P4
    • 15
    • core-libs
    • None
    • behavioral
    • low
    • 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.
    • Java API
    • SE

      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.

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

              Created:
              Updated:
              Resolved: