
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to remove element from ArrayList in Java?
The List interface is a member of Java Collections Framework. It extends Collection and stores a sequence of elements. ArrayList and LinkedList are the most commonly used implementations of List interface. List provides user a precise control over where an element to be inserted in the List. These elements can be accessed by their index and are searchable.
The List provides two remove() methods to remove an element from list by providing the index of element or element itself.
Remove using Index.
E remove(int index)
Notes
Removes the element at the specified position in this list.
Shifts any subsequent elements to the left (subtracts one from their indices).
Returns the element that was removed from the list.
Parameters
index - The index of the element to be removed.
Returns
The element previously at the specified position
Throws
UnsupportedOperationException - If the remove operation is not supported by this list.
IndexOutOfBoundsException - If the index is out of range (index < 0 || index >= size()).
Remove using Object.
boolean remove(Object o)
Notes
Removes the first occurrence of the specified element from this list, if it is present.
If this list does not contain the element, it is unchanged.
Removes the element with the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))) (if such an element exists).
Returns true if this list contained the specified element (or equivalently, if this list changed as a result of the call).
Parameters
o - Element to be removed from this list, if present.
Returns
True if this list contained the specified element.
Throws
ClassCastException - If the type of the specified element is incompatible with this list (optional).
NullPointerException - If the specified element is null and this list does not permit null elements (optional).
UnsupportedOperationException - if the remove operation is not supported by this list.
Example 1
Following is the example showing the usage of remove() method to remove an element by index −
package com.tutorialspoint; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class CollectionsDemo { public static void main(String[] args) { List<Integer> list = new ArrayList<>(Arrays.asList(1,2,3,4,5,6,7,8,9,0)); System.out.println("List: " + list); // remove item at index 5, list.remove(5); // updated list is not having 6 System.out.println("Updated List: " + list); } }
Output
This will produce the following result −
List: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] Updated List: [1, 2, 3, 4, 5, 7, 8, 9, 0]
Example 2
Following is the example showing the usage of remove() method to remove an element by passing the element −
package com.tutorialspoint; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class CollectionsDemo { public static void main(String[] args) { List<Integer> list = new ArrayList<>(Arrays.asList(1,2,3,4,5,6,7,8,9,0)); System.out.println("List: " + list); // remove the item 7, list.remove(Integer.valueOf(7)); // updated list is not having 7 System.out.println("Updated List: " + list); } }
Output
This will produce the following result −
List: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] Updated List: [1, 2, 3, 4, 5, 6, 8, 9, 0]
- Related Articles
- How to remove an element from ArrayList in Java?
- How to remove an element from ArrayList or, LinkedList in Java?
- Java Program to Remove Repeated Element from An ArrayList
- How to remove duplicates from an ArrayList in Java?
- Remove an element from an ArrayList using the ListIterator in Java
- How to remove a SubList from an ArrayList in Java?
- Java Program to Remove duplicate elements from ArrayList
- How to remove the redundant elements from an ArrayList object in java?
- Remove duplicate items from an ArrayList in Java
- Remove all elements from the ArrayList in Java
- Retrieve an element from ArrayList in Java
- How to remove all elements of ArrayList in Java?
- How to remove an element from an array in Java
- How to remove an element from a Java List?
- How to remove duplications from arraylist for listview in Android?
