This post will discuss how to append elements at the end of a List in Java.

1. Using List.add() method

The standard solution to append an element to the end of a list is using the List.add() method, which takes the element to be inserted. Following is a simple example demonstrating the usage of this method:

Download  Run Code

Output:

[A, B, C, D, E]

 
The List interface provides an overloaded version of the add() method, which additionally takes the index at which the specified element is to be inserted. To insert at the end, pass the list’s size as an index to it.

Download  Run Code

Output:

[A, B, C, D, E]

2. Using List.addAll() method

If you want to insert multiple elements at the end of a list, the addAll() method might come in handy. It appends all elements in the collection to the end of the list, in the iteration order. For example,

Download  Run Code

Output:

[A, B, C, D, E, F, G]

 
If you want to insert only a single element, you can pass a singleton collection.

Download  Run Code

Output:

[A, B, C, D, E]

3. Using Deque.addLast() method

The Deque data structure is optimized to insert an element at the front and end in constant time. All Deque implementations contain the addLast() method, which adds the specified element at the end of the deque.

Download  Run Code

Output:

[A, B, C, D, E]

That’s all about appending elements at the end of a List in Java.