Initialization of an ArrayList in one line example
This tutorials demonstrates how to initialize a list or arraylist in one line in different ways.
ArrayList
is:List<String> planets = new ArrayList<String>();
planets.add("Earth");
planets.add("Mars");
planets.add("Venus");
The following examples demonstrate how to create and initialize a List
or ArrayList
in a single line of code.
Method 1: Creating a singleton list
Creates an immutable list containing only the specified object. The returned list is serializable.
List<String> planets = Collections.singletonList("Earth");
Method 2a: Creating and initialize a list in one line
Creates a fixed-size list backed by the specified array. This method acts as bridge between array-based and collection-based APIs, in combination with Collection.toArray(). The returned list is serializable and implements RandomAccess. This method also provides a convenient way to create a fixed-size list initialized to contain several elements:
List<String> planets = Arrays.asList("Earth", "Mars", "Venus");
Method 2b: Creating and initialize a list in one line and static import
We can statically import the asList()
import static java.util.Arrays.asList;
List<String> planets = asList("Earth", "Mars", "Venus");
Method 3a: Create and initialize an arraylist in one line
Constructs a list containing the elements of the specified collection, in the order they are returned by the collection’s iterator.
List<String> planets = new ArrayList<String>(Arrays.asList("Earth", "Mars", "Venus"));
Method 3b: Create and initialize an arraylist in one line and static import
We can statically import the asList()
import static java.util.Arrays.asList;
List<String> planets = new ArrayList<String>(asList("Earth", "Mars", "Venus"));
Method 4: Create and initialize an arraylist using anonymous inner class
Using an anonymous inner class with an instance initializer (also known as an “double brace initialization”).
List<String> planets = new ArrayList<String>() {{
add("Earth");
add("Mars");
add("Venus");
}};
Method 5a: Create and initialize a list using Java 8
Stream.of()
returns a sequential ordered stream whose elements are the specified values. Collectors.toList()
returns a Collector that accumulates the input elements into a new List. There are no guarantees on the type, mutability, serializability, or thread-safety of the List returned; if more control over the returned List is required, use toCollection(Supplier).
List<String> planets = Stream
.of("Earth", "Mars", "Venus")
.collect(Collectors.toList());
Method 5b: Create and initialize an arraylist using Java 8
Stream.of()
returns a sequential ordered stream whose elements are the specified values. Collectors.toCollection()
returns a Collector that accumulates the input elements into a new Collection, in encounter order. The Collection is created by the provided factory.
ArrayList<String> planets = Stream
.of("Earth", "Mars", "Venus")
.collect(Collectors.toCollection(ArrayList::new));
Method 6a: Create and initialize a list using Java 8 static import
We can statically import the of()
and toList()
methods.
import static java.util.stream.Stream.of;
import static java.util.stream.Collectors.toList;
List<String> planets = of("Earth", "Mars", "Venus").collect(toList());
Method 7b: Create and initialize an arraylist using Java 8 static import
We can statically import the of()
and toCollection()
methods.
import static java.util.stream.Stream.of;
import static java.util.stream.Collectors.toCollection;
ArrayList<String> planets = of("Earth", "Mars", "Venus").collect(toCollection(ArrayList::new));
wow, this is actually very informative
You can also use Google Guava:
import org.python.google.common.collect.Lists;
List<String> conditions = Lists.newArrayList("Earth", "Mars", "Venus");