How to remove the last element from List in Java?
In this article you will learn how to remove last element in your Collection-Type List. This includes ArrayList, LinkedList, Vector and many more which implement the Collection interface List. Beside of the Java standard API calls you will learn also other alternatives.
Table of Contents
1. Standard Java API Collections
Java in his early days in the 90s had in the market a very good reputation about his good and easy to use API. Especially as Sun introduced the Collections API for handling the typical Collection types of Java: Lists, Sets and Maps.
1.1. Fast and hard removal of the last element
You do not care what the last element is. You simply want to remove the last entry. So you ask yourself,
How do I remove the last element from a list?
The fastest way is to use the remove(pList.size()-1) of the List interface to remove the last entry.
import java.util.ArrayList; import java.util.List; public class RemoveLastElementFromList { public static void main(String[] args) { List lList = new ArrayList<>(); RemoveLastElementFromList lMyClass = new RemoveLastElementFromList(); lMyClass.addTestData(lList); lMyClass.removeLastElement(lList); } public void removeLastElement(List<?> pList) { // get the index of the last entry from the list int lLastIndex = -1; if (pList.isEmpty()) { // nothing to remove } else { lLastIndex = pList.size() - 1; // if you do not need the last element anymore pList.remove(lLastIndex); } } public void addTestData(List pList) { pList.add("first"); pList.add("second"); pList.add("third"); pList.add("fourth"); } }
1.2. Remove the last entry from the list but use the removed element for further processing
You do not care what the last element is. You simply want to remove the last entry.
public Object removeLastElementWithReturn(List<?> pList) { Object lResult = null; // get the index of the last entry from the list int lLastIndex = -1; if (pList.isEmpty()) { // nothing to remove } else { lLastIndex = pList.size() - 1; // the the reference of the removed element lResult = pList.remove(lLastIndex); } return lResult; }
1.3. More examples for specific List types
In the further examples you will see more examples how to remove from ArrayList, LinkedList and the "old" Vectors.
1.3.1. Remove the last entry from the ArrayList
The trick is pretty simple. Since the ArrayList implements the List-interface, which we used previously you simply can pass the ArrayList instance to our previous methods. Here the example.
public void exampleArrayList() { ArrayList lList = new ArrayList(); addTestData(lList); removeLastElement(lList); }
1.3.2. Remove the last entry from the LinkedList
Since the LinkedList implements the List-interface, we have the same case like with the ArrayList. Here the example.
public void exampleLinkedList() { LinkedList lList = new LinkedList(); addTestData(lList); removeLastElement(lList); }
1.3.3. Remove the last entry from the Vector
Since the Vector implements the List-interface, we have the same case like with the ArrayList. Here the example.
public void exampleVector() { Vector lList = new Vector(); addTestData(lList); removeLastElement(lList); }
1.4. You ask yourself: How to remove the last element of a Set?
Since the Set is defining that your collection has exactly one unique entry in the Set, it is not so easy to tell, what element is the last one. Depending on the implementation of the Set, your set may return different values as the first and of course last element. E.g. you will find in a TreeSet the method TreeSet.last() but this method is missing on a HashSet.
Especially if you read carefully the javadoc of a TreeSet, you will find in the description that the TreeSet is using the Comperator to order the Set contents in a natural ordering, there the HashSet does not give you any guarantee on the ordering of the Set.
2. Finally, which one to use?
The first approach, you simply remove the last entry. The second approach helps you to process the list and to keep track of which elements you have removed to process the "stack" of work.
Read more with similar articles: