开发者

What is the replacement for Sets.newConcurrentHashSet()?

开发者 https://www.devze.com 2022-12-16 01:26 出处:网络
I am upgrading from Google Collections 0.9 to 1.0. It seems Sets.newConcurrentHashSet() is no longer available. I was using it in the following construct:

I am upgrading from Google Collections 0.9 to 1.0. It seems Sets.newConcurrentHashSet() is no longer available. I was using it in the following construct:

public static <K, V> Multimap<K, V> newConcurrentMultimap()
{
    return Multimaps.newMultimap( new ConcurrentHashMap<K, Collection<V>>(), new Supplier<Collection<V>>()
    {
        @Override
        public Collection<V> get()
        {
             return Sets.<V>new开发者_如何学PythonConcurrentHashSet();
         }
    } );
}

What would be the best replacement for Sets.newConcurrentHashSet() ?

Edit: The reason for this construct is to have a multimap that is safe to read and write from multiple threads. It is used in a mostly-read scenario (and will be read a lot).


Sets.newConcurrentHashSet was withdrawn in 1.0rc1 (commit log). I don't know the reason for the withdrawal, but you could use the implementation yourself:

Sets.newSetFromMap(new ConcurrentHashMap<V, Boolean>());


Try to use MapMaker and then use newSetFromMap()


You can use

Collections.newSetFromMap(new ConcurrentHashMap<>());
0

精彩评论

暂无评论...
验证码 换一张
取 消