
- 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 do I remove multiple elements from a list in Java?
The List provides removeAll() method to remove all elements of a list that are part of the collection provided.
boolean removeAll(Collection<?> c)
Parameters
c − Collection containing elements to be removed from this list.
Returns
True if this list changed as a result of the call
Throws
UnsupportedOperationException − If the removeAll operation is not supported by this list.
ClassCastException − If the class of an element of this list is incompatible with the specified collection (optional).
NullPointerException − If this list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null.
Example
Following is the example showing the usage of removeAll() method −
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(0,1,2,3,4,5,6,7,8,9)); List<Integer> list1 = new ArrayList<>(Arrays.asList(6,7,8,9)); System.out.println("List: " + list); list.removeAll(list1); System.out.println("Updated List: " + list); } }
Output
This will produce the following result −
List: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Updated List: [0, 1, 2, 3, 4, 5]
- Related Articles
- How do you remove multiple items from a list in Python?
- How do I insert elements in a Java list?
- How do I insert all elements from one list into another in Java?
- How can I get elements from a Java List?
- Java program to remove duplicates elements from a List
- How do I insert elements at a specific index in Java list?
- How do I recursively remove consecutive duplicate elements from an array?
- How do I empty a list in Java?
- How do I search a list in Java?
- How do you remove duplicates from a list in Python?
- How do I remove elements not matching conditions in MongoDB?
- How can I find elements in a Java List?
- How do I remove a property from a JavaScript object?
- Remove elements from a linked list using Javascript
- How to remove an element from a Java List?

Advertisements