HashSet removeAll() method in java

In this tutorial we will learn about hashset removeAll() method in Java. removeAll() method of HashSet class removes from this set all of its elements that are contained in the given collection.

Syntax :

public boolean removeAll(Collection<?> c){

   }
  • Parameters – c collection containing elements to be removed from this Set.
  • Returns Value-  It return true if this set changed as a result of the call.
  • Exception –  It throws following exceptions :
    UnsupportedOperationException – if the removeAll operation is not supported by this set
    ClassCastException – if the class of an element of this set is incompatible with the specified collection.
    NullPointerException – if this set contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null.

Example 1 : Java HashSet removeAll() method – Elements Present

In this example , We will create HashSet of String and add elements into HashSet using add() method. We shall then use removeAll() method to remove elements from HashSet .

import java.util.HashSet;

public class HashSeteemoveAll {

	public static void main(String[] args) {

		// Constructs a new, empty set
		HashSet<String> hashSet = new HashSet<String>();

		// add element in it
		hashSet.add("java");
		hashSet.add("welcomes");
		hashSet.add("you");
		hashSet.add("to");
		hashSet.add("learn");

		// display the element
		System.out.println("element in hashset =" + hashSet);

		HashSet<String> hashSet2 = new HashSet<String>();
		hashSet2.add("you");
		hashSet2.add("learn");

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

		// removing element using removeAll() method
		boolean isElementsRemoved = hashSet.removeAll(hashSet2);

		System.out.println("is Elements removed From HashSet ? "
				+ isElementsRemoved);

		System.out.println("After Remove elements hashset is =" + hashSet);

	}

}

Output:

element in hashset =[welcomes, java, learn, to, you]
Collection Elements to be removed : [learn, you]
is Elements removed From HashSet ? true
After Remove elements hashset is =[welcomes, java, to]

Example 2 : Java HashSet removeAll() method – Elements not Present

We will initialize HashSet with String elements. Then we shalll use removeAll() method but elements of provide collection is not present in HashSet.

import java.util.HashSet;

public class HashSeteemoveAll1 {
	
	public static void main(String[] args) {
		
		// Constructs a new, empty set
		HashSet<String> hashSet = new HashSet<String>();

		// add element in it
		hashSet.add("java");
		hashSet.add("welcomes");
		hashSet.add("you");
		hashSet.add("to");
		hashSet.add("learn");

		// display the element
		System.out.println("element in hashset =" + hashSet);

		HashSet<String> hashSet2 = new HashSet<String>();
		hashSet2.add("android");
		hashSet2.add("php");
		
		System.out.println("Collection Elements to be removed : "+ hashSet2 );
	
		//removing element using removeAll() method
		boolean isElementsRemoved =  hashSet.removeAll(hashSet2);
		
	System.out.println("is Elements removed From HashSet ? "+ isElementsRemoved );

	System.out.println("After Remove elements hashset is =" + hashSet);

	}

}

Output:

element in hashset =[welcomes, java, learn, to, you]
Collection Elements to be removed : [android, php]
is Elements removed From HashSet ? false
After Remove elements hashset is =[welcomes, java, learn, to, you]

Example 3 : Java HashSet removeAll() method – NullPointerException

Let’s see example of HashSet removeAll() method for NullPointerException. We will provides null collection to removeAll() method , then it will throw null pointer exception .

import java.util.HashSet;

public class HashSeteemoveAll2 {

	public static void main(String[] args) {

		// Constructs a new, empty set
		HashSet<String> hashSet = new HashSet<String>();

		// add element in it
		hashSet.add("java");
		hashSet.add("welcomes");
		hashSet.add("you");
		hashSet.add("to");
		hashSet.add("learn");

		// display the element
		System.out.println("element in hashset =" + hashSet);

		HashSet<String> hashSet2 = null;

		System.out.println("Collection Elements to be removed : " + hashSet2);
		try {
			// removing element using removeAll() method
			boolean isElementsRemoved = hashSet.removeAll(hashSet2);

			System.out.println("is Elements removed From HashSet ? "
					+ isElementsRemoved);

			System.out.println("After Remove elements hashset is =" + hashSet);
		} catch (NullPointerException e) {
			System.out.println("Exception : " + e);
		}

	}

}

Output:

element in hashset =[welcomes, java, learn, to, you]
Collection Elements to be removed : null
Exception : java.lang.NullPointerException

In this tutorial we learned hashset removeall() method in java . You can learn more java HashSet example for practice .

A Guide to HashSet

Leave a Reply

Your email address will not be published. Required fields are marked *

64 − = 55