Add Multiple Items to an Java ArrayList

1. Overview of ArrayList

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

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

2. AddAll

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

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

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.

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

3. Collections.addAll

The Collections class consists exclusively of static methods that operate on or return 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.

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

This version of Java opens our possibilities by adding new tools. The one we’ll explore in the next examples is 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.ofNullable(source).ifPresent(target::addAll)

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

5. Conclusion

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

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

Related posts:

Giới thiệu Json Web Token (JWT)
Java Program to Perform Quick Sort on Large Number of Elements
Java Program to Check if a Given Set of Three Points Lie on a Single Line or Not
Guide to the Volatile Keyword in Java
Lập trình hướng đối tượng (OOPs) trong java
Java Program to find the maximum subarray sum O(n^2) time(naive method)
Java Program to Generate All Possible Combinations of a Given List of Numbers
Java Stream Filter with Lambda Expression
Quick Guide to Spring Bean Scopes
Java Program to Implement PrinterStateReasons API
Java Program to Implement Interpolation Search Algorithm
Java Program to Give an Implementation of the Traditional Chinese Postman Problem
Jackson Exceptions – Problems and Solutions
HttpClient Timeout
How to Find an Element in a List with Java
How to Get the Last Element of a Stream in Java?
Instance Profile Credentials using Spring Cloud
Function trong Java 8
Default Password Encoder in Spring Security 5
Java 8 Stream API Analogies in Kotlin
Java Program to Permute All Letters of an Input String
Spring REST API + OAuth2 + Angular (using the Spring Security OAuth legacy stack)
An Intro to Spring Cloud Zookeeper
JUnit5 Programmatic Extension Registration with @RegisterExtension
New Features in Java 10
Java Program to Implement LinkedBlockingQueue API
Java Program to Implement the Vigenere Cypher
Java Program to Implement K Way Merge Algorithm
Spring Cloud Series – The Gateway Pattern
The DAO with Spring and Hibernate
Java Program to Implement Circular Doubly Linked List
Spring Boot - Tomcat Port Number