I’m sure this is a very simple question but please take a look at the code sample below:
final ImmutableSortedSet<String> notOk = ImmutableSortedSet.naturalOrder().build();
final ImmutableSortedSet<String> ok = new ImmutableSortedSet.Builder<String>(Ordering.natural()).build();
final ImmutableList<String> typicalGuava = ImmutableList.of("one", "two");
I’m just wondering what is the proper way to use the naturalOrder() method in the first example? In that example Java cannot infer the type so you get a “type mismatch” error.
With a series of chained calls like that, the compiler is unable to infer the type argument for the call to
naturalOrder()since its result is not immediately assigned to something it can use for inference.You can write
or