Hacker News new | ask | show | jobs
by fi0660 6179 days ago
For initializing lists there is a less verbose expression in Java:

  List<Integer> list = Arrays.asList(1,2,3,4,5);
1 comments

. . . except that doesn't get you a modifiable list, it just wraps a List interface around the array that's implicitly created by the var-arg, so it's a trick you have to be careful with.

Personally I often end up writing little helpers like: List<T> list(T... args) { return new ArrayList<T>(Arrays.asList(args)) } usually in test code. A built-in List and Map-initialization syntax is really something every decent programming language ought to have.

(Consider using the Google collections library if possible. It already has that in the form of classes like Lists, Maps, etc that provide builders and factory methods.)