Hidden Features of Java

Hidden Features of Java - ArrayList / instance initializers clarification

List<Integer> numbers = new ArrayList<Integer>(){{ add(1); add(2); }};

I don't really recommend this approach, as it creates an (anonymous) class for no good reason.

Use either:

List<Integer> numbers = Arrays.asList(1, 2);

or

List<Integer> numbers = new ArrayList<Integer>(Arrays.asList(1, 2));

For 2 levels, you can use:

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

With a static import you could even reduce it to this, if you really want to:

List<List<Integer>> numbers = asList(asList(1, 2), asList(2,3,4));

Hidden Features IntelliJ IDEA

I love syntax-aware selection. Control + W.

When I'm forced to work with other IDEs, that key sequence usually causes files to close. It's a nasty negative feedback loop.



Related Topics



Leave a reply



Submit