Add Multiple Items to an Java ArrayList – 在Java数组列表中添加多个项目

最后修改: 2018年 7月 29日

中文/混合/英文(键盘快捷键:t)

1. Overview of ArrayList

1.ArrayList的概述

In this quick tutorial, we’ll show to how to add multiple items to an already initialized ArrayList.

在这个快速教程中,我们将展示如何向一个已经初始化的ArrayList添加多个项目。

For an introduction to the use of the ArrayList, please refer to this article here.

关于ArrayList的使用介绍,请参考此处的文章

2. AddAll

2.添加所有

First of all, we’re going to introduce a simple way to add multiple items into an ArrayList.

首先,我们要介绍一种简单的方法,将多个项目添加到ArrayList中。

First, we’ll be using addAll(), which takes a collection as its argument:

首先,我们将使用addAll(),它需要一个集合作为其参数。

List<Integer> anotherList = Arrays.asList(5, 12, 9, 3, 15, 88);
list.addAll(anotherList);

It’s important to keep in mind that the elements added in the first list will reference the same objects as the elements in anotherList.

重要的是要记住,第一个列表中添加的元素将引用与anotherList中的元素相同的对象。

For that reason, every amends made in one of these elements will affect both lists.

由于这个原因,对其中一个要素的每一次修改都会影响到这两份名单。

3. Collections.addAll

3.Collections.addAll

The Collections class consists exclusively of static methods that operate on or return collections.

Collections类完全由操作或返回集合的静态方法组成。

One of them is addAll, which needs a destination list and the items to be added may be specified individually or as an array.

其中之一是addAll,它需要一个目标列表,要添加的项目可以单独指定或作为一个数组。

Here it’s an example of how to use it with individual elements:

这里是一个如何使用单个元素的例子。

List<Integer> list = new ArrayList<>();
Collections.addAll(list, 1, 2, 3, 4, 5);

And another one to exemplify the operation with two arrays:

还有一个是以两个数组为例说明操作。

List<Integer> list = new ArrayList<>();
Integer[] otherList = new Integer[] {1, 2, 3, 4, 5};
Collections.addAll(list, otherList);

Similarly to the way explained in the above section, the contents of both lists here will refer to the same objects.

与上节解释的方式类似,这里的两个列表的内容将指的是同一个对象。

4. Using Java 8

4.使用Java 8

This version of Java opens our possibilities by adding new tools. The one we’ll explore in the next examples is Stream:

这个版本的Java通过增加新的工具,打开了我们的可能性。我们将在接下来的例子中探讨的是Stream

List<Integer> source = ...;
List<Integer> target = ...;

source.stream()
  .forEachOrdered(target::add);

The main advantages of this way are the opportunity to use skip and filters. In the next example we’re going to skip the first element:

这种方式的主要优点是有机会使用跳过和过滤器。在下一个例子中,我们要跳过第一个元素。

source.stream()
  .skip(1)
  .forEachOrdered(target::add);

It’s possible to filter the elements by our necessities. For instance, the Integer value:

可以根据我们的需要来过滤这些元素。例如,整数值。

source.stream()
  .filter(i -> i > 10)
  .forEachOrdered(target::add);

Finally, there are scenarios where we want to work in a null-safe way. For those ones, we can use Optional:

最后,在有些情况下,我们希望以无效安全的方式工作。对于这些情况,我们可以使用Optional

Optional.ofNullable(source).ifPresent(target::addAll)

In the above example, we’re adding elements from source to target by the method addAll.

在上面的例子中,我们通过addAll方法从sourcetarget添加元素。

5. Conclusion

5.总结

In this article, we’ve explored different ways to add multiple items to an already initialized ArrayList.

在这篇文章中,我们探讨了向已经初始化的ArrayList添加多个项目的不同方法。

As always, code samples can be found over on GitHub.

一如既往,代码样本可以在GitHub上找到over