hashset remove all code example
Example 1: HashSet removeAll() method in java
example on removeAll() method for NullPointerException
import java.util.HashSet;
public class HashSetRemoveAllMethodExample
{
public static void main(String[] args)
{
try
{
HashSet<Integer> hs1 = new HashSet<Integer>();
hs1.add(2);
hs1.add(4);
hs1.add(6);
hs1.add(8);
hs1.add(10);
System.out.println("HashSet before using removeAll() method: " + hs1);
HashSet<Integer> hs2 = null;
System.out.println("Elements to be removed: " + hs2);
System.out.println("Trying to pass null: ");
hs1.removeAll(hs2);
System.out.println("HashSet after using removeAll() method: " + hs1);
}
catch(NullPointerException ex)
{
System.out.println("Exception: " + ex);
}
}
}
Example 2: HashSet removeAll() method in java
import java.util.HashSet;
public class HashSetRemoveAllMethodExample
{
public static void main(String[] args)
{
try
{
HashSet<Integer> hs1 = new HashSet<Integer>();
hs1.add(2);
hs1.add(4);
hs1.add(6);
hs1.add(8);
hs1.add(10);
System.out.println("HashSet before using removeAll() method: " + hs1);
HashSet<Integer> hs2 = new HashSet<Integer>();
hs2.add(2);
hs2.add(4);
hs2.add(6);
System.out.println("Elements to be removed: " + hs2);
hs1.removeAll(hs2);
System.out.println("HashSet after using removeAll() method: " + hs1);
}
catch(NullPointerException ex)
{
System.out.println("Exception: " + ex);
}
}
}