I'm using Google Guava r08 and JDK 1.6.0_23.
I want to create an ImmutableSortedMap
using a builder. I know I can create the builder like this:
ImmutableSortedMap.Builder<Integer, String> b1 =
new ImmutableSortedMap.Builder<Integer, String>(Ordering.natural());
and then use that to build maps, for example:
ImmutableSortedMap<Integer, String> map =
b1.put(1, "one").put(2, "two").put(3, "three").build();
I noticed that class ImmutableSortedMap
has a method naturalOrder()
that returns a Builder
with natural ordering. However, when I try to call this method, I get strange errors. For example, this gives a strange "; expected" error:
// Does not compile
ImmutableSortedMap.Builder<Integer, String> b2 =
ImmutableSortedMap<Integer, String>.naturalOrder();
What is the correct syntax to call the naturalOrder()
method?
The API documentation of the method mentions some compiler bug. Does that have anything to do with this method not working?
edit
MForster's answer is good. But when leaving off the generics, I can't do it "in one go":
// Doesn't work, can't infer the types properly
ImmutableSortedMap<Integer, String> map =
ImmutableSortedMap.naturalOrder().put(1, "one").put(2, "two").put(3, "three").build();
This does work:
ImmutableSortedMap<Integer, String> map =
ImmutableSortedMap.<Integer, String>naturalOrder().put(1, "one").put(2, "two").put(3, "three").build();