Java List Initialization in One Line
1. Introduction In this quick tutorial, we'll investigate how can we initialize a List using one-liners. 2. Create From an Array We can create a List from an array and thanks to array literals we can initialize them in one line: List<String> list = Arrays.asList( new String[]{ "foo" , "bar" }); We can trust the varargs mechanism to handle the array creation. By that, we can write more concise and readable code: @Test public void givenArraysAsList_thenInitialiseList() { List<String> list = Arrays.asList( "foo" , "bar" ); assertTrue(list.contains( "foo" )); } The result instance of this code