Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Collections replaceAll() Method in Java with Examples
Next article icon

Java Collection removeAll() Method with Examples

Last Updated : 24 Oct, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

The removeAll() method of Java Collection removes those elements from the collection that are contained in the collection which is given as an argument to function.

Syntax

boolean removeAll(Collection<?> c);

Parameters:

  • c is the collection containing elements that are removed from the calling collection.

Return Type:

It returns a boolean value. It returns true if the calling collection was modified by the removeAll() method else it returns false.

Example of Collection removeAll() method

Below is the implementation of the above method:

Java
// Java Program to demonstrate // Collection removeAll() method import java.io.*; import java.util.ArrayList; import java.util.Collection; import java.util.List;  // Driver Class class Main {     // main function     public static void main(String[] args)     {         Collection<Integer> list1 = new ArrayList<>();         list1.add(1);         list1.add(2);         list1.add(3);         list1.add(4);          System.out.println("Original List: " + list1);          // ArrayList         Collection<Integer> list2 = new ArrayList<>();         list2.add(2);         list2.add(4);          System.out.println(             "List containing elements to be removed from the calling collection(list1): "             + list2);          boolean isRemoved = list1.removeAll(list2);          System.out.println("Elements removed from List1: "                            + isRemoved);         System.out.println("Modified list1 after deletion: "                            + list1);     } } 

Output
Original List: [1, 2, 3, 4]  List containing elements to be removed from the calling collection(list1): [2, 4]  Elements removed from List1: true  Modified list1 after deletion: [1, 3]    

Example 2:

Another example where the collection passed as an argument doesn't contain any element which could be deleted in original collection.

Java
import java.io.*; import java.util.ArrayList; import java.util.List;  // Driver Class class GFG {       // main function     public static void main(String[] args)     {                  // Initiating list1         List<Integer> list1 = new ArrayList<Integer>();         list1.add(1);         list1.add(2);         list1.add(3);         list1.add(4);          System.out.println("Original List: " + list1);            // Initiating list2         List<Integer> list2 = new ArrayList<Integer>();         list2.add(7);         list2.add(8);          System.out.println(             "List containing elements to be removed from the calling collection(list1): "             + list2);            // Using removeAll() method         boolean isRemoved = list1.removeAll(list2);          System.out.println("Elements removed from List1: "                            + isRemoved);         System.out.println("Modified list1 after deletion: "                            + list1);     } } 

Output
Original List: [1, 2, 3, 4]  List containing elements to be removed from the calling collection(list1): [7, 8]  Elements removed from List1: false  Modified list1 after deletion: [1, 2, 3, 4]    

Next Article
Collections replaceAll() Method in Java with Examples

S

saswatdas121
Improve
Article Tags :
  • Java
  • Geeks Premier League
  • Java-Collections-Class
  • Java-Classes
  • Geeks Premier League 2023
Practice Tags :
  • Java

Similar Reads

  • Collections replaceAll() Method in Java with Examples
    The replaceAll() method of java.util.Collections class is used to replace all occurrences of one specified value in a list with another. More formally, replaces with newVal each element e in the list such that oldVal == null ? e==null : oldVal.equals(e) Note: This method has no effect on the size of
    3 min read
  • AbstractCollection removeAll() method in Java with Example
    The Java.util.AbstractCollection.removeAll(Collection col) method is used to remove all the elements from the AbstractCollection, present in the collection specified. Syntax: AbstractCollection.removeAll(Collection col) Parameters: This method accepts a mandatory parameter col which is the collectio
    2 min read
  • AbstractCollection remove() Method in Java with Examples
    The remove(Object O) method of Java AbstractCollection is to remove a particular element from a Collection. Syntax: AbstractCollection.remove(Object O) Parameters: The parameter O is of the type of Collection and specifies the element to be removed from the collection. Return Value: This method retu
    2 min read
  • ArrayList removeAll() Method in Java with Examples
    The removeAll() method of the ArrayList class in Java is used to remove all elements of an ArrayList that are specified in another collection or within the same list itself. Example 1: Here, we use the removeAll() method to remove all elements from an ArrayList by passing the same list as an argumen
    3 min read
  • Collection add() Method in Java with Examples
    The add(E element) of java.util.Collection interface is used to add the element 'element' to this collection. This method returns a boolean value depicting the successfulness of the operation. If the element was added, it returns true, else it returns false. Syntax: Collection.add(E element) Paramet
    4 min read
  • Collections.singleton() method in Java with example
    java.util.Collections.singleton() method is a java.util.Collections class method. It creates a immutable set over a single specified element. An application of this method is to remove an element from Collections like List and Set. Syntax: public static Set singleton(T obj) and public static List si
    3 min read
  • List removeAll() method in Java with Examples
    This method is used to remove all the elements present in the collection from the specified list. Syntax: boolean removeAll(Collection c) Parameters: This method has only argument, collection of which elements are to be removed from the given list. Returns: This method returns True if elements are r
    2 min read
  • CompoundName remove() method in Java with Examples
    The remove() method of a javax.naming.CompoundName class is used to remove a component situated at position posn from this compound name. The position posn is passed as a parameter to this method. This method removes the component of this compound name object at position 'posn' and components presen
    2 min read
  • Collection clear() method in Java with Examples
    The collection clear() method of Java Collection Interface clears the Collection upon which it is called. After this method is called, the collection will be empty as it removes all the elements from the collection. This method does not take any parameter and does not return any value. Example: Java
    3 min read
  • Collections fill() method in Java with Examples
    The fill() method of java.util.Collections class is used to replace all of the elements of the specified list with the specified element. This method runs in linear time. Syntax: public static void fill(List list, T obj) Parameters: This method takes following argument as parameter list - the list t
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences