Remove first element from a list in Java
This post will discuss how to remove the first element from a list in Java.
1. Using List.remove() method
A common solution is to remove the element at the specific position in the list is using the remove() method. It works by shifting any subsequent elements to the left. Note that UnsupportedOperationException will be thrown if the remove() method is called on an unmodifiable list.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; class Main { public static void main(String[] args) { List<Integer> collection = IntStream.rangeClosed(1, 5) .boxed().collect(Collectors.toList()); collection.remove(0); System.out.println(collection); } } |
Output:
[2, 3, 4, 5]
The remove() method throws an IndexOutOfBoundsException if the list is empty. To handle it, you can create a utility method with size check on the list.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.ArrayList; import java.util.List; class Main { public static void removeFirst(List<Integer> collection) { if (collection != null && collection.size() > 0) { collection.remove(0); } } public static void main(String[] args) { List<Integer> collection = new ArrayList<>(); removeFirst(collection); System.out.println(collection); } } |
Output:
[]
2. Using List.clear() method
Alternatively, you can get a view of the first element in the list which is backed by the original list, and call the clear() method on it. This can be done using the subList() method, as shown below.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; class Main { public static void main(String[] args) { List<Integer> collection = IntStream.rangeClosed(1, 5) .boxed().collect(Collectors.toList()); collection.subList(0, 1).clear(); System.out.println(collection); } } |
Output:
[2, 3, 4, 5]
To avoid an exception when the list is empty, perform an additonal check for an empty list before getting the sublist.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.ArrayList; import java.util.Arrays; import java.util.List; class Main { public static void removeFirst(List<Integer> collection) { if (collection != null && !collection.isEmpty()) { collection.subList(0, 1).clear(); } } public static void main(String[] args) { List<Integer> collection = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5)); removeFirst(collection); System.out.println(collection); } } |
Output:
[2, 3, 4, 5]
3. Using Deque.removeFirst() method
If you happen to use a Deque (ArrayDeque, LinkedList, etc.), removing the first element from it can be achieved in constant time. The idea is to use the removeFirst() method, which removes and returns the first element of the deque.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import java.util.ArrayDeque; import java.util.Deque; import java.util.stream.Collectors; import java.util.stream.IntStream; class Main { public static void main(String[] args) { Deque<Integer> deque = IntStream.rangeClosed(1, 5) .boxed().collect(Collectors.toCollection(ArrayDeque::new)); deque.removeFirst(); System.out.println(deque); } } |
Output:
[2, 3, 4, 5]
The removeFirst() method throws an exception when the deque is empty. To be exception-safe, you can use the pollFirst() method instead, which returns null if the deque is empty.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import java.util.Deque; import java.util.LinkedList; import java.util.stream.Collectors; import java.util.stream.IntStream; class Main { public static void main(String[] args) { Deque<Integer> linkedList = IntStream.rangeClosed(1, 5) .boxed().collect(Collectors.toCollection(LinkedList::new)); linkedList.pollFirst(); System.out.println(linkedList); } } |
Output:
[2, 3, 4, 5]
That’s all about removing the first element from 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 :)