jiweigang1 / google-collections

Automatically exported from code.google.com/p/google-collections
Apache License 2.0
0 stars 0 forks source link

Using create to create a Multimap and using synchronizedMultimaps breaks generics #222

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Let's create a Multimap

Multimap<Integer, Integer> map = HashMultimap.create();

Works - great.

Now let's make it synchronized, hmm, let's try

Multimap<Integer, Integer> map =
Multimaps.synchronizedMultimap(HashMultimap.create());

Oops, that doesn't work. I assume since create doesn't have the type
inference to figure it out with the synchronized call in between.

Ok, the docs on Multimaps.synchronizedMultimap imply that you can do new
HashMultimap<K,V>(). But for rc2, that is private and you can only use
create().

So the only thing I could do to make it compile is:

Multimap<Integer, Integer> map = (Multimap<Integer, Integer>) (Multimap)
Multimaps.synchronizedMultimap(HashMultimap.create());

Yuck!

Am I missing something?

Thanks,
Senthil.

Original issue reported on code.google.com by senthilv...@gmail.com on 17 Aug 2009 at 9:54

GoogleCodeExporter commented 9 years ago
Unfortunately, Java generics have some gigantic warts, and you've bumped into 
one of 
them.

Your only alternative is

Multimaps.synchronizedMultimap(HashMultimap.<Integer, Integer>create())

Original comment by kevin...@gmail.com on 17 Aug 2009 at 9:59