4

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>newConcurrentHashSet();
         }
    } );
}

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).

miken32
  • 42,008
  • 16
  • 111
  • 154
Wim Deblauwe
  • 25,113
  • 20
  • 133
  • 211

3 Answers3

9

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>());
Ben Lings
  • 28,823
  • 13
  • 72
  • 81
  • This is exactly what I have done for now. I am just wondering if it will do what I hope since the javadoc states that: "The multimap is not threadsafe when any concurrent operations update the multimap, even if map and the instances generated by factory are." I do not like to use the synchronized wrapper, since that no longer makes it concurrent but synchronized. – Wim Deblauwe Jan 13 '10 at 13:14
  • 1
    You're wondering whether it works because the javadoc tells you specifically that it doesn't work? :-) – Kevin Bourrillion Jan 13 '10 at 19:53
  • 1
    It was withdrawn in a "one step back, two steps forward" manner; since we have plans to support concurrent Sets with a decent fraction of the options you get for maps with `MapMaker`, we didn't want to also be stuck with that `newConcurrentHashSet` method for all eternity. Sorry that said functionality hasn't appeared yet. – Kevin Bourrillion Jan 13 '10 at 19:58
1

Try to use MapMaker and then use newSetFromMap()

nanda
  • 24,458
  • 13
  • 71
  • 90
-1

You can use

Collections.newSetFromMap(new ConcurrentHashMap<>());
Henry Ecker
  • 34,399
  • 18
  • 41
  • 57