Initialization of an ArrayList in one line example

This tutorials demonstrates how to initialize a list or arraylist in one line in different ways.
The traditional way to create and initialize anArrayList 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 theasList()

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).

ArrayList<String> planets = Stream
        .of("Earth", "Mars", "Venus")
        .collect(Collectors.toCollection(ArrayList::new));
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));
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值