[PATCH] 8202521: Add overloaded methods of Map#compute, Map#computeIfAbsent, Map#computeIfPresent (original) (raw)

Jacob Glickman jhg023 at bucknell.edu
Thu May 3 17:43:10 UTC 2018


Hello! This is my first patch, so I apologize in advance if I've done anything incorrectly (including botching the formatting of the mercurial diff below).

I think it would be beneficial to have an overloaded Map.computeIfAbsent method where the computed value does not depend on the key; for that reason, I submitted 1. By taking a Supplier<? extends Value>, this will allow users to utilize method references where applicable. For example, if they create a Map<String, Collection<Integer>>, then the following can be used when computing values:

map.computeIfAbsent("key", ArrayList::new).add(1);

I originally planned to add overloaded methods for Map.computeIfPresent and Map.compute as well, but it would make more sense for them to take a Function (instead of a Supplier) if the computed value does not depend on the key. If this patch is successful, then I'll go forward with that planned change. To clarify, an overloaded Map.computeIfAbsent method was the only change, along with the unit test.

I used JMH to benchmark my implementation versus an inlined implementation (not calling the existing Map.computeIfAbsent with the result of the supplier), and the difference was negligible (maybe the compiler inlined it?). Please let me know if I should inline it so I can change my implementation.


HG changeset patch

User jhg023

Date 1525366367 25200

Thu May 03 09:52:47 2018 -0700

Node ID 395def2871e8d077b382d722efc59b38373327d1

Parent ea246151be08995543d0c9281099608bc9207a19

8202521: Add overloaded methods of Map#compute, Map#computeIfAbsent, Map#computeIfPresent Summary: Added "Map.computeIfAbsent(K key, Supplier<? extends V> supplier)" Reviewed-by: N/A Contributed-by: Jacob Glickman <jhg023 at bucknell.edu>

diff -r ea246151be08 -r 395def2871e8 src/java.base/share/classes/java/util/Hashtable.java --- a/src/java.base/share/classes/java/util/Hashtable.java Wed May 02 11:21:27 2018 -0700 +++ b/src/java.base/share/classes/java/util/Hashtable.java Thu May 03 09:52:47 2018 -0700 @@ -29,6 +29,7 @@ import java.util.function.BiConsumer; import java.util.function.Function; import java.util.function.BiFunction; +import java.util.function.Supplier; import jdk.internal.misc.SharedSecrets;

/** @@ -1054,6 +1055,21 @@ * {@inheritDoc} * *

This method will, on a best-effort basis, throw a

supplier) {

diff -r ea246151be08 -r 395def2871e8 src/java.base/share/classes/java/util/Map.java --- a/src/java.base/share/classes/java/util/Map.java Wed May 02 11:21:27 2018 -0700 +++ b/src/java.base/share/classes/java/util/Map.java Thu May 03 09:52:47 2018 -0700 @@ -28,6 +28,7 @@ import java.util.function.BiConsumer; import java.util.function.BiFunction; import java.util.function.Function; +import java.util.function.Supplier; import java.io.Serializable;

/** @@ -1010,6 +1011,85 @@ }

 /**

mapped

supplier

this

if the

basis,

synchronization

document

href="{@docRoot}/java.base/java/util/Collection.html#optional-restrictions">optional)

value

href="{@docRoot}/java.base/java/util/Collection.html#optional-restrictions">optional)

key

href="{@docRoot}/java.base/java/util/Collection.html#optional-restrictions">optional)

@@ -301,6 +301,27 @@ assertSame(map.get(null), EXTRA_VALUE, "not expected value"); }

values=withNull")

Map<IntegerEnum, String> map) {

expected result");

EXTRA_VALUE, "not mapped to result");

value");

EXTRA_VALUE, "not mapped to result");

values=all") public void testComputeIfAbsent(String description, Map<IntegerEnum, String> map) { // 1 -> 1 @@ -321,6 +342,25 @@ }

 @Test(dataProvider = "Map<IntegerEnum,String> rw=true keys=all

values=all")

Map<IntegerEnum, String> map) {

expected, description);

EXTRA_VALUE);

values=all") public void testComputeIfAbsentNullFunction(String description, Map<IntegerEnum, String> map) { assertThrowsNPE(() -> map.computeIfAbsent(KEYS1, null)); } @@ -370,7 +410,7 @@ assertThrowsNPE(() -> map.computeIfPresent(KEYS1, null)); }

values=withNull")

values=withNull") public void testComputeNulls(String description, Map<IntegerEnum, String> map) { assertTrue(map.containsKey(null), "null key absent"); assertNull(map.get(null), "value not null");



More information about the core-libs-dev mailing list