I have a method that expects a Set parameter. I want to pass in an empty set, and I don’t want any side effects on the Set.
I can do this with collections by passing in:
Collections.unmodifiableSet(Sets.newHashSet())
But I want to pass in:
ImmutableSet.of()
If I do the former a Set<Object> is created and I get “method not applicable for args Set’ error. If I do the latter I get ImmutableSet<Object> is created and I get similar error.
This works:
Collections.unmodifiableSet(new HashSet<String>())
… but seems ugly, and I want to find a Google Collections way.
Try this:
This will work too:
This syntax is useful for manually specifying type arguments any time the type inference fails. 🙂