A suggestion: Map.checkAndGet

Boaz Nahum boaznahum at gmail.com
Wed Oct 31 10:15:35 PDT 2012


So still we have a problem(s), because:

public static <T, U> Mapper<T, U> forMap(Map<? super T, ? extends U> map, U
defaultValue) {
        Objects.requireNonNull(map);

        return t -> map.containsKey(t) ? map.get(t) : defaultValue;
  }

forMap knows nothing about concurrent map, so:
1. It walks twice on tree.
2. ?:  is not atomic - but this may be considered as non issue because map
may not modified during 'forMap'

Boaz


On Wed, Oct 31, 2012 at 3:53 PM, Doug Lea <dl at cs.oswego.edu> wrote:

> On 10/31/12 07:44, David Holmes wrote:
> > Boaz pointed out to me that my suggestion assumes Maps with no null
> values.
>
> As an aside, ConcurrentHashMap has never allowed null keys or values,
> which allows "null" to consistently always mean "nothing there".
> This initially made atomicity guarantees possible (no need for
> separate calls to containsKey), and has made it much easier to
> make JDK8 version lambda-friendly, including computeIfAbsent,
> getValueOrDefault, etc methods, plus bulk operations, without ever
> relying on "Optional" (which is another way of saying "nothing there").
> For preview release API (that has not yet been fully integrated with
> Streams), see:
>
>
> http://gee.cs.oswego.edu/dl/jsr166/dist/docs/java/util/concurrent/ConcurrentHashMap.html
>
> -Doug
>
>


More information about the lambda-dev mailing list