ConcurrentHashMap/ConcurrentMap/Map.compute

Joe Bowbeer joe.bowbeer at gmail.com
Tue Jan 8 22:47:10 PST 2013


In the javadoc for Map.compute in b72, there is an error in the sample
snippet:

http://download.java.net/lambda/b72/docs/api/java/util/Map.html

Attempts to compute a mapping for the specified key and its current mapped
> value (or null if there is no current mapping). For example, to either
> create or append a String msg to a value mapping:
>  map.compute(key, (k, v, msg) -> (v == null) ? msg : v.concat(msg))


Error: BiFunction does not accept three arguments.  In particular, msg is
extraneous.  It should be defined in the lexical scope?


Btw, I pondered how to efficiently use compute() or merge() to simulate a
multi-map and I eventually gave up.

I eventually wrote the following, which accumulates lists of anagrams,
given a list of words:

r.lines().forEach(s -> map.computeIfAbsent(key(s), k -> new
ArrayList<>()).add(s));


Where key() returns the canonical key for a given word, and r is a
BufferedReader for the dictionary file.

The following line prints the lists of anagrams:

map.values().stream().filter(v -> v.size() > 1).forEach(v ->
System.out.println(v));



On Mon, Dec 17, 2012 at 4:48 AM, Doug Lea <dl at cs.oswego.edu> wrote:

>
> A fixed-up successfully jdk8-javadoc'ed version of Map is now at
>  http://gee.cs.oswego.edu/dl/**wwwtmp/apis/Map.java<http://gee.cs.oswego.edu/dl/wwwtmp/apis/Map.java>
> with displayable javadoc at:
>  http://gee.cs.oswego.edu/dl/**wwwtmp/apis/java/util/Map.html<http://gee.cs.oswego.edu/dl/wwwtmp/apis/java/util/Map.html>
>
> Comments? Complaints? If not, feel free to integrate.
>
> (Arne: thanks for pointing out that "merge" was mangled.)
>
> Side note about this and a few other upcoming cases:
> We must keep j.u.c sources independent, so normally commit
> to jsr166 and then integrate into openjdk. But ad-hoc
> mechanics seem the only way to deal with updates to
> existing files. Handing off is likely nicer than directly
> committing into lambda.
>
> -Doug
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.openjdk.java.net/pipermail/lambda-libs-spec-experts/attachments/20130108/09e1d500/attachment.html 


More information about the lambda-libs-spec-experts mailing list