Quantcast
Channel: Initialization of an ArrayList in one line - Stack Overflow
Browsing latest articles
Browse All 37 View Live

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 Article


Answer 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 Article

Answer 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 Article

Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article

Answer 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 Article


Answer 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 Article


Answer 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 Article

Answer 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 Article

Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article


Answer by Ant20 for Initialization of an ArrayList in one line

Try with this code line:Collections.singletonList(provider)

View Article

Answer 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 Article


Answer 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 Article

Answer 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 Article

Answer 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

Answer by Ran Adler for Initialization of an ArrayList in one line

List<String> names = Arrays.asList("2", "@2234", "21", "11");

View Article


Answer 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 Article


Answer 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 Article

Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article

Answer 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 Article

Answer 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 Article



Answer 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 Article

Answer 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 Article

Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article

Initialization 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 Article


Answer 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

Browsing latest articles
Browse All 37 View Live




<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>