简体   繁体   中英

Is there a concurrent version of this definition Java Guava collections Map

Is there a concurrent version of this definition Guava collections Map ?

ListMultimap<DuplicateKey,Integer> map =
    Multimaps.newListMultimap(
    Maps.<DuplicateKey, Collection<Integer>>newTreeMap(),
    new Supplier<List<Integer>>() {
        public List<Integer> get() {
            return Lists.newArrayList();
        }
    });

There's no concurrent multimap implementation, but you can wrap it with Multimaps.synchronizedListMultimap view, which:

Returns a synchronized (thread-safe) multimap backed by the specified multimap.

In your case:

ListMultimap<DuplicateKey,Integer> synchronizedMultimap = 
    Multimaps.synchronizedListMultimap(map);

Read complete javadoc for caveats regarding synchronized access to views:

In order to guarantee serial access, it is critical that all access to the backing multimap is accomplished through the returned multimap.

Note that there'll be no general-purpose concurrent multimap implementation in Guava itself, according to issue #135 on Github .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM