I wanted to create a list of options for testing purposes. At first, I did this:
ArrayList places = new ArrayList();
places.add(\
With Guava you can write:
ArrayList<String> places = Lists.newArrayList("Buenos Aires", "Córdoba", "La Plata");
In Guava there are also other useful static constructors. You can read about them here.
You can use the below statements:
String [] arr = {"Sharlock", "Homes", "Watson"};
List<String> names = Arrays.asList(arr);
Try with this code line:
Collections.singletonList(provider)
interestingly no one-liner with the other overloaded Stream::collect
method is listed
ArrayList<String> places = Stream.of( "Buenos Aires", "Córdoba", "La Plata" ).collect( ArrayList::new, ArrayList::add, ArrayList::addAll );
If you need a simple list of size 1:
List<String> strings = new ArrayList<String>(Collections.singletonList("A"));
If you need a list of several objects:
List<String> strings = new ArrayList<String>();
Collections.addAll(strings,"A","B","C","D");
Simply use below code as follows.
List<String> list = new ArrayList<String>() {{
add("A");
add("B");
add("C");
}};