How about java.util.Arrays.asList ?
You can simply transfer the contents as var-the arg :
List<String> planets = Arrays.asList( "Mercury", "Venus", "Earth", "Mars" );
Keep in mind that you can also pass an array:
String[] ps = new String[]{ "Mercury", "Venus", "Earth", "Mars" }; List<String> planets = Arrays.asList( ps ); ] { "Mercury", "Venus", "Earth", "Mars"}; String[] ps = new String[]{ "Mercury", "Venus", "Earth", "Mars" }; List<String> planets = Arrays.asList( ps );
but it is "supported" by the array in that the change in the contents of the array will be reflected in the list:
String[] ps = new String[]{ "Mercury", "Venus", "Earth", "Mars" }; List<String> planets = Arrays.asList( ps ); ps[3] = "Terra"; assert planets.get(3).equals( "Terra" ); ] { "Mercury", "Venus", "Earth", "Mars"}; String[] ps = new String[]{ "Mercury", "Venus", "Earth", "Mars" }; List<String> planets = Arrays.asList( ps ); ps[3] = "Terra"; assert planets.get(3).equals( "Terra" ); ps); String[] ps = new String[]{ "Mercury", "Venus", "Earth", "Mars" }; List<String> planets = Arrays.asList( ps ); ps[3] = "Terra"; assert planets.get(3).equals( "Terra" );
source share