Append elements at the end of a List in Java
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:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.util.ArrayList; import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { List<String> alphabets = new ArrayList<>(Arrays.asList("A", "B", "C", "D")); String e = "E"; alphabets.add(e); System.out.println(alphabets); } } |
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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import java.util.*; class Main { public static void main(String[] args) { List<String> alphabets = new ArrayList<>(Arrays.asList("A", "B", "C", "D")); String e = "E"; alphabets.add(alphabets.size(), e); System.out.println(alphabets); } } |
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,
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import java.util.*; class Main { public static void main(String[] args) { List<String> alphabets = new ArrayList<>(Arrays.asList("A", "B", "C", "D")); List<String> collection = Arrays.asList("E", "F", "G"); alphabets.addAll(collection); System.out.println(alphabets); } } |
Output:
[A, B, C, D, E, F, G]
If you want to insert only a single element, you can pass a singleton collection.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import java.util.*; class Main { public static void main(String[] args) { List<String> alphabets = new ArrayList<>(Arrays.asList("A", "B", "C", "D")); String e = "E"; alphabets.addAll(Collections.singleton(e)); System.out.println(alphabets); } } |
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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.util.ArrayDeque; import java.util.Arrays; import java.util.Deque; class Main { public static void main(String[] args) { Deque<String> alphabets = new ArrayDeque<>(Arrays.asList("A", "B", "C", "D")); String e = "E"; alphabets.addLast(e); // or add() method System.out.println(alphabets); } } |
Output:
[A, B, C, D, E]
That’s all about appending elements at the end of a List in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)