I'd like to create an (immutable) Multiset in Guava that has a single entry element
with occurrences occurrences
, both of which are not known at compile time.
What I've come up with is this:
ImmutableMultiset.<X>builder().addCopies(element, occurrences).build()
I guess I was looking for a method like this:
public static ImmutableMultiset<X> ImmutableMultiset.nOccurrencesOf(
X element, int occurrences){}
or:
public static ImmutableMultiset<X> Multisets.singletonMultiset(
X element, int occurrences){}
Is there any method I have overlooked that makes the above code shorter?
Guava contributor here.
Stick with the builder. It already addresses the problem quite simply, and in a single line; it's probably not a common enough case to require its own special method.