A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/abstractset-removeall-method-in-java-with-examples/ below:

AbstractSet removeAll() Method in Java

AbstractSet removeAll() Method in Java

Last Updated : 11 Jul, 2025

The removeAll() method of the Java AbstractSet class is part of the Java Collections Framework. This method is used to remove all elements from the current set that are also contained in another collection or set.

Example 1: This example is used to remove all elements from the abstract set using the removeAll() method.

Java
// Java program to demonstrate 
// removeAll() method for Integer value 
import java.util.*;

public class Geeks 
{
    public static void main(String[] args) throws Exception 
    {
        try 
        {        
            // Creating object of AbstractSet<Integer>
            AbstractSet<Integer> as = new TreeSet<Integer>();

            // Populating set
            as.add(1);
            as.add(2);
            as.add(3);
            as.add(4);
            as.add(5);

            System.out.println("AbstractSet before "
            + "removeAll() operation: "
            + as);

            // Creating another object of ArrayList<Integer>
            Collection<Integer> l = new ArrayList<Integer>();
            l.add(1);
            l.add(2);
            l.add(3);

            System.out.println("Collection Elements"
            + " to be removed: "
            + l);

            // Removing elements from AbstractSet
            // specified in list
            // using removeAll() method
            as.removeAll(l);

            // print arrlist1
            System.out.println("AbstractSet after "
            + "removeAll() operation: "
            + as);
        }

        catch (NullPointerException e) 
        {
            System.out.println("Exception thrown: " + e);
        }
    }
}

Output
AbstractSet before removeAll() operation: [1, 2, 3, 4, 5]
Collection Elements to be removed: [1, 2, 3]
AbstractSet after removeAll() operation: [4, 5]
Syntax

boolean removeAll(Collection<?> c);

Exceptions: 

Example 2: This program shows how to use removeAll() Method on AbstractSet with Integer Values and handling NullPointerException.

Java
// Java program to demonstrate 
// removeAll() method for Integer value 
import java.util.*; 

public class Geeks
 { 
	public static void main(String[] args) throws Exception 
	{ 
		try { 

			// Creating object of AbstractSet<Integer> 
			AbstractSet<Integer> 
			as = new TreeSet<Integer>(); 

			as.add(1); 
			as.add(2); 
			as.add(3); 
			as.add(4); 
			as.add(5); 

			System.out.println("AbstractSet before "
			+ "removeAll() operation: "
			+ as); 

			// Creating another object of ArrayList<Integer> 
			Collection<Integer> 
			l = new ArrayList<Integer>(); 
			l = null; 

			System.out.println("Collection Elements"
			+ " to be removed: "
			+ l); 

			// Removing elements from AbstractSet 
			// specified in list 
			// using removeAll() method 
			as.removeAll(l); 

			System.out.println("AbstractSet after "
			+ "removeAll() operation: "
			+ as); 
		} 

		catch (NullPointerException e) { 
			System.out.println("Exception thrown:" + e); 
		} 
	} 
} 

Output
AbstractSet before removeAll() operation: [1, 2, 3, 4, 5]
Collection Elements to be removed: null
Exception thrown:java.lang.NullPointerException


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4