ConcurrentHashMap/ConcurrentMap/Map.compute (original) (raw)
Paul Sandoz paul.sandoz at oracle.com
Wed Jan 9 01:15:58 PST 2013
- Previous message: ConcurrentHashMap/ConcurrentMap/Map.compute
- Next message: Cancelable streams
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On Jan 9, 2013, at 7:47 AM, Joe Bowbeer <joe.bowbeer at gmail.com> wrote:
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));
Have you tried using the latest groupBy functionality? e.g.:
Map<String, Collection> anagrams = r.lines().parallel().reduce(groupBy(s -> key(s)));
It would be interesting to compare performance of reduce vs. reduceUnordered and the concurrent groupBy leveraging CHM.
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));
Little style tip:
forEach(System.out::println)
Paul.
- Previous message: ConcurrentHashMap/ConcurrentMap/Map.compute
- Next message: Cancelable streams
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the lambda-libs-spec-observers mailing list