Answer by Old Firm for Initialization of an ArrayList in one line
I suspect the original question relates to a desire to declare and initialize the ArrayList in one line and avoid having to create separate code to acheive the initialization. Thus, the answer lies in...
View ArticleAnswer by Ajay Singh for Initialization of an ArrayList in one line
There are multiple ways to create and initialize list in one line. Some examples://Using Double brace initialization, creates a new (anonymous) subclass of ArrayListList<String> list1 = new...
View ArticleAnswer by PRANAV SINGH for Initialization of an ArrayList in one line
Simplest way : you can use this approach to add multiple elements to any type of collection like ArrayList and HashSetArrayList<String> allViews = new...
View ArticleAnswer by Akila for Initialization of an ArrayList in one line
Using Arrays.asList("Buenos Aires", "Córdoba", "La Plata"); is correct. but Any calls to Arrays.asList() with zero arguments or only one argument could be replaced with either a call to...
View ArticleAnswer by Kaplan for Initialization of an ArrayList in one line
interestingly no one-liner with the other overloadedStream::collectmethod is listedArrayList<String> places = Stream.of( "Buenos Aires", "Córdoba", "La Plata" ).collect( ArrayList::new,...
View ArticleAnswer by Stefan Reich for Initialization of an ArrayList in one line
Why not make a simple utility function that does this?static <A> ArrayList<A> ll(A... a) { ArrayList l = new ArrayList(a.length); for (A x : a) l.add(x); return l;}"ll" stands for "literal...
View ArticleAnswer by Mohit Tyagi for Initialization of an ArrayList in one line
In Java 9 we can easily initialize an ArrayList in a single line:List<String> places = List.of("Buenos Aires", "Córdoba", "La Plata");orList<String> places = new...
View ArticleAnswer by yegor256 for Initialization of an ArrayList in one line
You can use StickyList from Cactoos:List<String> names = new StickyList<>("Scott Fitzgerald", "Fyodor Dostoyevsky");
View ArticleAnswer by rashedcs for Initialization of an ArrayList in one line
You can use the below statements:Code Snippet:String [] arr = {"Sharlock", "Homes", "Watson"};List<String> names = Arrays.asList(arr);
View ArticleAnswer by Henok T for Initialization of an ArrayList in one line
Here is another way:List<String> values = Stream.of("One", "Two").collect(Collectors.toList());
View ArticleAnswer by Naman for Initialization of an ArrayList in one line
With java-9 and above, as suggested in JEP 269: Convenience Factory Methods for Collections, this could be achieved using collection literals now with -List<String> list = List.of("A", "B",...
View ArticleAnswer by user85421 for Initialization of an ArrayList in one line
Java 9 has the following method to create an immutable list as documented (or documented as unmodifiableJava 10+):List<String> places = List.of("Buenos Aires", "Córdoba", "La Plata");which is...
View ArticleAnswer by user_3380739 for Initialization of an ArrayList in one line
Here is code by abacus-common// ArrayListList<String> list = N.asList("Buenos Aires", "Córdoba", "La Plata");// HashSetSet<String> set = N.asSet("Buenos Aires", "Córdoba", "La Plata");//...
View ArticleAnswer by Akash Manngroliya for Initialization of an ArrayList in one line
Yes with the help of Arrays you can initialize array list in one line,List<String> strlist= Arrays.asList("aaa", "bbb", "ccc");
View ArticleAnswer by Charif DZ for Initialization of an ArrayList in one line
The best way to do it:package main_package;import java.util.ArrayList;public class Stackkkk { public static void main(String[] args) { ArrayList<Object> list = new ArrayList<Object>();...
View ArticleAnswer by Ant20 for Initialization of an ArrayList in one line
Try with this code line:Collections.singletonList(provider)
View ArticleAnswer by ViliusK for Initialization of an ArrayList in one line
Collections.singletonList(messageBody)If you'd need to have a list of one item!Collections is from java.util package.
View ArticleAnswer by Donald Raab for Initialization of an ArrayList in one line
With Eclipse Collections you can write the following:List<String> list = Lists.mutable.with("Buenos Aires", "Córdoba", "La Plata");You can also be more specific about the types and whether they...
View ArticleAnswer by Richard B for Initialization of an ArrayList in one line
About the most compact way to do this is:Double array[] = { 1.0, 2.0, 3.0};List<Double> list = Arrays.asList(array);
View ArticleAnswer by Manoj Kumar for Initialization of an ArrayList in one line
For me, Arrays.asList() is the best and most convenient one. I always like to initialize that way.If you are a beginner in Java Collections then I would like you to refer ArrayList initialization.
View Article