Martin Schröder
Martin Schröder

Reputation: 4591

How to create a LinkedHashMultiset<String>?

I'm starting to use the Guava classes and stumbling about the missing constructors.

I have a method that gets a LinkedHashMultiset<String>. How do I create a new argument in the method call?

class.method(LinkedHashMultiset.create()); makes the compiler complain that there is no method for class.method(LinkedHashMultiset<Object>).

And class.method((LinkedHashMultiset<String)LinkedHashMultiset.create()) fails due to an impossible cast.

Upvotes: 0

Views: 336

Answers (2)

JB Nizet
JB Nizet

Reputation: 691765

As with any generic method. EIther the type is deduced by the compiler (type inference):

LinkedHashMultiset<String> set = LinkedHashMultiset.create();

or it's not, and you have to specify the type:

LinkedHashMultiset.<String>create()

Upvotes: 6

Olivier Gr&#233;goire
Olivier Gr&#233;goire

Reputation: 35437

One way is the following:

class.method(LinkedHashMultiset.<String>create());

One other is this:

Multiset<String> multiset = LinkedHashMultiset.create();
class.method(multiset);

Upvotes: 5

Related Questions