Add element at the beginning of a List in Java
This post will discuss how to add an item at the beginning of a List in Java.
1. Using List.add() method
The standard solution to insert a specified item at the specified position in the list is to use the add(index, element) method in the List interface, which takes the index and the element to be inserted.
|
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> colors = new ArrayList<>(Arrays.asList("Red", "Blue", "Brown", "Purple")); String color = "Yellow"; colors.add(0, color); System.out.println(colors); } } |
Output:
[Yellow, Red, Blue, Brown, Purple]
2. Using Deque.addFirst() method
The add() method takes O(n) time, since it shifts all the elements to the right to make place for a new element. Inserting an item at the beginning can be done in O(1) time if you happen to use a Deque (ArrayDeque, LinkedList, etc.). It offers the addFirst() method, which inserts the specified element at the front of the deque.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.util.ArrayDeque; import java.util.Arrays; import java.util.Deque; class Main { public static void main(String[] args) { Deque<String> colors = new ArrayDeque<>( Arrays.asList("Red", "Blue", "Brown", "Purple")); String color = "Yellow"; colors.addFirst(color); System.out.println(colors); } } |
Output:
[Yellow, Red, Blue, Brown, Purple]
Note that the addFirst() method throws IllegalStateException if it fails to insert an element due to capacity restrictions. When using a capacity-restricted deque, using the offerFirst() method is generally preferable.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.Arrays; import java.util.Deque; import java.util.LinkedList; class Main { public static void main(String[] args) { Deque<String> colors = new LinkedList<>( Arrays.asList("Red", "Blue", "Brown", "Purple") ); String color = "Yellow"; colors.offerFirst(color); System.out.println(colors); } } |
Output:
[Yellow, Red, Blue, Brown, Purple]
3. Using Collections.reverse() method
The idea here is to reverse the list, insert the specified element at its end, and reverse the list again to get the desired order. This solution works, but it is not recommended for production code.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.util.*; class Main { public static void main(String[] args) { List<String> colors = new ArrayList<>(Arrays.asList("Red", "Blue", "Brown", "Purple")); String color = "Yellow"; Collections.reverse(colors); colors.add(color); Collections.reverse(colors); System.out.println(colors); } } |
Output:
[Yellow, Red, Blue, Brown, Purple]
That’s all about adding an item at the beginning 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 :)