
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Remove Element from a Java List
In Java, List is an interface that stores a sequence of elements of similar types. Like an array, elements in the list are stored at a specific index starting at 0.
Since we can access elements in the list through their index, and we can pass this index value to the remove() method (i.e., a basic method for removing elements), which will remove the element at the specified index.
We can remove an element from the List in Java:
Removing Element from a List Using remove() Method
The remove() method of the List interface is used to remove an element at the specified index. It accepts an index as a parameter and returns the removed element from the list.
Once the element is removed, it shifts any subsequent elements to the left (subtracts one from their indices).
Following is the syntax of the remove() method:
E remove(int index)
Here, an index is the position of an element to be removed.
Example
In the following example, we use the remove() method to remove an element at the specified index 1 from the list {2, 4, 6, 8, 10}:
import java.util.ArrayList; import java.util.List; public class removeElement { public static void main(String[] args) { //instantiating a List using ArrayList class List<Integer> even_nums = new ArrayList<>(); //adding element to it even_nums.add(2); even_nums.add(4); even_nums.add(6); even_nums.add(8); even_nums.add(10); System.out.println("The list before removing element: " + even_nums); int index = 1; System.out.println("The index index of the element to be removed: " + index); //using remove() method even_nums.remove(index); System.out.println("The list after removing element: " + even_nums); } }
The above program produces the following output:
The list before removing element: [2, 4, 6, 8, 10] The index index of the element to be removed: 1 The list after removing element: [2, 6, 8, 10]
Using remove(object) Method
The remove(object) method of the List interface is used to remove the first occurrence of the specified element (i.e., object) from the list if it is present.
It accepts an object (or element) as a parameter and returns true if the list contains the specified element.
Following is the syntax of the remove(object) method:
boolean remove(Object o)
Here, o is the element to be removed from the list, if present.
Example
In this example, we use the remove(object) method to remove the first occurrence of the specified element "Python" from the list {"Java", "JavaScript", "Python", "TypeScript"}:
import java.util.ArrayList; import java.util.List; public class removeElement { public static void main(String[] args) { //instantiating a List using ArrayList class List<String> languages = new ArrayList<>(); //adding element to it languages.add("Java"); languages.add("JavaScript"); languages.add("Python"); languages.add("TypeScript"); System.out.println("The list before removing element: " + languages); String lang = "Python"; System.out.println("The element needs to be removed: " + lang); //using remove(object) method languages.remove(lang); System.out.println("The list after removing element: " + languages); } }
Following is the output of the above program:
The list before removing element: [Java, JavaScript, Python, TypeScript] The element needs to be removed: Python The list after removing element: [Java, JavaScript, TypeScript]
Remove all Elements by using the clear() Method
The clear() method removes all the elements from the List. Once this method is invoked on the list, the list will be empty ().
Following is the syntax of the clear() method:
clear()
Example
The example below uses the clear() method to remove all elements from the List {1, 2, 3, 4, 5}:
import java.util.ArrayList; import java.util.List; public class removeElement { public static void main(String[] args) { //instantiating a List using ArrayList class List<Integer> list = new ArrayList<>(); //adding element to it list.add(1); list.add(2); list.add(3); list.add(4); list.add(5); System.out.println("The list before removing element: " + list); //using clear() method list.clear(); System.out.println("The list after removing element: " + list); } }
Below is the output of the above program:
The list before removing element: [1, 2, 3, 4, 5] The list after removing element: []