Reputation: 1599
I have always been using guava's collection utilities to create a list:
List<Integer> foo = Lists.newArrayList(1, 2, 3);
Lately I found the primitives utilities, which allows this:
List<Integer> bar = Ints.asList(1, 2, 3);
From the documentation, both foo
and bar
are mutable:
foo.set(0, 100);
bar.set(0, 100);
So what is the difference (if any) between the two, for primitive types like int?
Upvotes: 1
Views: 268
Reputation: 280141
Primitives don't work with generics. This
List<Integer> foo = Lists.newArrayList(1, 2, 3);
would incur the cost of boxing each int
value and wrapping the whole thing in an Integer[]
to be passed to newArrayList
. The List
returned is not fixed size.
This
List<Integer> bar = Ints.asList(1, 2, 3);
would only incur the cost of wrapping the three arguments in an int[]
as that's the parameter type. The List
returned is fixed size
Upvotes: 6