In this quick article, we’ll see how to remove the last element of a list in Java.

We can use the remove(int index) method of the List interface, which removes an element at the specified position in the list. To remove the last element, we need to pass the index of the last element, as shown below:

Download  Run Code

Output:

Original list: [A, B, C, D, E]
Modified list: [A, B, C, D]

 
The remove method is overloaded in the List interface. If all the list elements are distinct, and we know the last element, we can call the remove(Object o) method. It removes the first occurrence of the specified element from the list if present.

Download  Run Code

Output:

Original list: [A, B, C, D, E]
Modified list: [A, B, C, D]

 
Note it’s preferable to use a Deque instead of a List, which efficiently supports deletion at the end. Following is a simple example demonstrating deletion in Deque:

Download  Run Code

Output:

Original list: [A, B, C, D, E]
Modified list: [A, B, C, D]

That’s all about removing the last element of a List in Java.