I’ve been out of the loop on straight Java programming for a while, so this is probably obvious to a lot of people, and has been for a long time. But on the off chance that I’m not the only other idiot out there, I’ll post this.
The old way of instantiating a List in Java was like this:
List strings = Arrays.asList(new String[]{"foo","bar"});
In Java 5, templates were introduced, so I had to change to doing this:
List<String> strings = Arrays.asList(new String[]{"foo","bar"});
It just occurred to me that with variable-length methods, I can simply call asList like this:
List<String> strings = Arrays.asList("foo","bar");
my friend, this is very, very bad. Instantiating array to instantiate a list…
How else would you instantiate a static list? It’s only “bad” if the compiler doesn’t optimize it. Which it does.
The post demonstrates a way of doing this clearly in Java 5 syntax.