AbstractSet removeAll() Method in Java Last Updated : 11 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report 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); } } } OutputAbstractSet before removeAll() operation: [1, 2, 3, 4, 5] Collection Elements to be removed: [1, 2, 3] AbstractSet after removeAll() operation: [4, 5] Syntaxboolean removeAll(Collection<?> c);Parameters: This method takes collection c as a parameter containing elements to be removed from this set.Returns Value: This method returns true if this set changes as a result of the call.Exceptions: UnsupportedOperationException: This is thrown if the operation is not supported by this set.ClassCastException: This is thrown when the class of an element of this set is not compatible with the specified collection.NullPointerException: Thrown when this set has a null element but the collection doesn’t allow nulls, or if the collection is null.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); } } } OutputAbstractSet before removeAll() operation: [1, 2, 3, 4, 5] Collection Elements to be removed: null Exception thrown:java.lang.NullPointerException Comment More infoAdvertise with us Next Article Java HashSet clear() Method C chinmoy lenka Follow Improve Article Tags : Misc Java Java-Collections Java - util package Java-Functions +1 More Practice Tags : JavaJava-CollectionsMisc Similar Reads Java HashSet HashSet in Java implements the Set interface of Collections Framework. It is used to store the unique elements and it doesn't maintain any specific order of elements. Can store the Null values.Uses HashMap (implementation of hash table data structure) internally.Also implements Serializable and Clon 12 min read Java HashSet add() Method The HashSet add() method in Java is used to add a specific element to the set. This method will add the element only if the specified element is not present in the HashSet. If the element already exists, the method will not add it again. This method ensures that no duplicate elements are added to th 2 min read Java HashSet clear() Method The clear() method of the HashSet class in Java is used to clear all the elements from the HahSet. This method makes the HashSet empty but does not delete the HashSet itself. It simply removes all elements, leaving the set ready for reuse.Syntax of HashSet clear() Methodvoid clear()Key Points:After 1 min read Java HashSet contains() Method The contains() method of the HashSet class in Java is used to check if a specific element is present in the HashSet or not. So, mainly it is used to check if a set contains any particular element.Java// Java program to demonstrates the working of contains() import java.util.*; public class Geeks { p 1 min read HashSet remove() Method in Java The HashSet remove() method in Java is used to remove a specific element from the set if it is present.Note: HashSet and the remove() were introduced in JDK 1.2 as part of the Collections Framework and are not available in earlier versions of Java (JDK 1.0 and JDK 1.1).Example 1: Here, the remove() 2 min read Java HashSet iterator() Method The HashSet iterator() method in Java is used to return an iterator that can be used to iterate over the elements in a HashSet. HashSet does not maintain any specific order of its elements, so the elements are returned in random order when iterated over. The iterator() method provides a way to trave 3 min read Java HashSet isEmpty() Method The HashSet isEmpty() in Java is used to check if the HashSet is empty or not.Syntax of HashSet isEmpty() Methodboolean isEmpty()Return Type: This method returns "true" if the HashSet is empty, otherwise returns "false".Example: This example demonstrates how the isEmpty() method checks whether the g 1 min read Java HashSet size() Method The HashSet size() method in Java is used to get the size of the HashSet or the number of elements present in the HashSet. Syntax of HashSet size() Methodint size()Return Type: This method returns an integer value that represents the number of elements currently stored in the HashSet.Example: This e 1 min read Java HashSet clone() Method The HashSet clone() method in Java is used to return a shallow copy of the given HashSet. It just creates a copy of the set.Syntax of HashSet clone() Methodpublic Object clone()Return Type: This method returns a new HashSet object that contains the same element as the original set.Example: This exam 1 min read Java AbstractSet equals() Method The AbstractSet equals() Method in Java is used to check for equality between two sets. This method verifies whether the elements of one set are equal to the elements of another set.Syntax of AbstractSet equals() Methodpublic boolean equals(Object o)Parameter: The object "o" is to be compared for eq 1 min read Like