Answer by Ran Adler for Initialization of an ArrayList in one line
List<String> names = Arrays.asList("2", "@2234", "21", "11");
View ArticleAnswer by Mark for Initialization of an ArrayList in one line
Collection literals didn't make it into Java 8, but it is possible to use the Stream API to initialize a list in one rather long line:List<String> places = Stream.of("Buenos Aires", "Córdoba",...
View ArticleAnswer by user2801794 for Initialization of an ArrayList in one line
Simply use below code as follows.List<String> list = new ArrayList<String>() {{ add("A"); add("B"); add("C");}};
View ArticleAnswer by Paweł Adamski for Initialization of an ArrayList in one line
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.
View ArticleAnswer by Ozzy for Initialization of an ArrayList in one line
Like Tom said:List<String> places = Arrays.asList("Buenos Aires", "Córdoba", "La Plata");But since you complained of wanting an ArrayList, you should first know that ArrayList is a subclass of...
View ArticleAnswer by user439407 for Initialization of an ArrayList in one line
(Should be a comment, but too long, so new reply). As others have mentioned, the Arrays.asList method is fixed size, but that's not the only issue with it. It also doesn't handle inheritance very well....
View ArticleAnswer by Adrian for Initialization of an ArrayList in one line
Actually, it's possible to do it in one line:Arrays.asList(new MyClass[] {new MyClass("arg1"), new MyClass("arg2")})
View ArticleAnswer by abaelter for Initialization of an ArrayList in one line
public static <T> List<T> asList(T... a) { return new ArrayList<T>(a);}This is the implementation of Arrays.asList, so you could go withArrayList<String> arr =...
View ArticleAnswer by Dawg for Initialization of an ArrayList in one line
In Java, you can't doArrayList<String> places = new ArrayList<String>( Arrays.asList("Buenos Aires", "Córdoba", "La Plata"));As was pointed out, you'd need to do a double brace...
View ArticleAnswer by Randyaa for Initialization of an ArrayList in one line
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...
View ArticleAnswer by Christoffer Hammarström for Initialization of an ArrayList in one line
The simple answerJava 9 or later:List<String> strings = List.of("foo", "bar", "baz");List.of(...) will give you an immutableList, so it cannot be changed.Which is what you want in most cases...
View ArticleAnswer by George for Initialization of an ArrayList in one line
import com.google.common.collect.ImmutableList;....List<String> places = ImmutableList.of("Buenos Aires", "Córdoba", "La Plata");
View ArticleAnswer by Jordão for Initialization of an ArrayList in one line
You could create a factory method:public static ArrayList<String> createArrayList(String ... elements) { ArrayList<String> list = new ArrayList<String>(); for (String element :...
View ArticleAnswer by Tom for Initialization of an ArrayList in one line
It would be simpler if you were to just declare it as a List - does it have to be an ArrayList?List<String> places = Arrays.asList("Buenos Aires", "Córdoba", "La Plata");Or if you have only one...
View ArticleAnswer by coobird for Initialization of an ArrayList in one line
Actually, probably the "best" way to initialize the ArrayList is the method you wrote, as it does not need to create a new List in any way:ArrayList<String> list = new...
View ArticleInitialization of an ArrayList in one line
I wanted to create a list of options for testing purposes. At first, I did this:ArrayList<String> places = new ArrayList<String>();places.add("Buenos...
View ArticleAnswer by Amar kumar Nayak for Initialization of an ArrayList in one line
You can use of() method from java 9 onwardsList<String> places = List.of("Buenos Aires","Córdoba","La Plata");of() methods create immutable list.If you want to modifie you got exception.
View Article