LinkedHashSet removeAll() method in Java with Example Last Updated : 14 May, 2019 Comments Improve Suggest changes Like Article Like Report The removeAll() method of java.util.LinkedHashSet class is used to remove from this set all of its elements that are contained in the specified collection. Syntax: public boolean 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 changed as a result of the call. Exception: This method throws 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. Below are the examples to illustrate the removeAll() method. Example 1: Java // Java program to demonstrate // removeAll() method for Integer value import java.util.*; public class GFG1 { public static void main(String[] argv) throws Exception { try { // Creating object of LinkedHashSet<Integer> LinkedHashSet<Integer> set1 = new LinkedHashSet<Integer>(); // Populating set1 set1.add(1); set1.add(2); set1.add(3); set1.add(4); set1.add(5); // print set1 System.out.println("LinkedHashSet before " + "removeAll() operation : " + set1); // Creating another object of LinkedHashSet<Integer> LinkedHashSet<Integer> set2 = new LinkedHashSet<Integer>(); set2.add(1); set2.add(2); set2.add(3); // print set2 System.out.println("Collection Elements" + " to be removed : " + set2); // Removing elements from set // specified in set2 // using removeAll() method set1.removeAll(set2); // print set1 System.out.println("LinkedHashSet after " + "removeAll() operation : " + set1); } catch (NullPointerException e) { System.out.println("Exception thrown : " + e); } } } Output: LinkedHashSet before removeAll() operation : [1, 2, 3, 4, 5] Collection Elements to be removed : [1, 2, 3] LinkedHashSet after removeAll() operation : [4, 5] Example 2: For NullPointerException Java // Java program to demonstrate // removeAll() method for Integer value import java.util.*; public class GFG1 { public static void main(String[] argv) throws Exception { try { // Creating object of LinkedHashSet<Integer> LinkedHashSet<Integer> set1 = new LinkedHashSet<Integer>(); // Populating set1 set1.add(1); set1.add(2); set1.add(3); set1.add(4); set1.add(5); // print set1 System.out.println("LinkedHashSet before " + "removeAll() operation : " + set1); // Creating another object of LinkedHashSet<Integer> LinkedHashSet<Integer> set2 = null; // print set2 System.out.println("Collection Elements" + " to be removed : " + set2); System.out.println("\nTrying to pass " + "null as a specified element\n"); // Removing elements from set // specified in set2 // using removeAll() method set1.removeAll(set2); // print set1 System.out.println("LinkedHashSet after " + "removeAll() operation : " + set1); } catch (NullPointerException e) { System.out.println("Exception thrown : " + e); } } } Output: LinkedHashSet before removeAll() operation : [1, 2, 3, 4, 5] Collection Elements to be removed : null Trying to pass null as a specified element Exception thrown : java.lang.NullPointerException Comment More infoAdvertise with us Next Article LinkedHashSet toString() method in Java with Example C code_r Follow Improve Article Tags : Java Java-Collections Java - util package Java-Functions java-LinkedHashSet +1 More Practice Tags : JavaJava-Collections Similar Reads Java LinkedHashSet LinkedHashSet in Java implements the Set interface of the Collection Framework. It combines the functionality of a HashSet with a LinkedList to maintain the insertion order of elements. Stores unique elements only.Maintains insertion order.Provides faster iteration compared to HashSet.Allows null el 8 min read LinkedHashSet retainAll() method in Java with Example The retainAll() method of java.util.LinkedHashSet class is used to retain from this set all of its elements that are contained in the specified collection. Syntax: public boolean retainAll(Collection c) Parameters: This method takes collection c as a parameter containing elements to be retained from 3 min read LinkedHashSet hashCode() method in Java with Example The hashCode() method of LinkedHashSet in Java is used to get the hashCode value for this instance of the LinkedHashSet. It returns an integer value which is the hashCode value for this instance of the LinkedHashSet. Syntax: public int hashCode() Parameters: This function has no parameters. Returns: 2 min read LinkedHashSet removeAll() method in Java with Example The removeAll() method of java.util.LinkedHashSet class is used to remove from this set all of its elements that are contained in the specified collection. Syntax: public boolean removeAll(Collection c) Parameters: This method takes collection c as a parameter containing elements to be removed from 3 min read LinkedHashSet toString() method in Java with Example The toString() method of Java LinkedHashSet is used to return a string representation of the elements of the Collection.The String representation comprises a set representation of the elements of the Collection in the order they are picked by the iterator closed in square brackets[].This method is u 2 min read LinkedHashSet toArray() method in Java with Example The toArray() method of Java LinkedHashSet is used to form an array of the same elements as that of the LinkedHashSet. Basically, it copies all the element from a LinkedHashSet to a new array. Syntax: Object[] arr = LinkedHashSet.toArray() Parameters: The method does not take any parameters. Return 2 min read LinkedHashSet equals() method in Java with Example The equals() method of java.util.LinkedHashSet class is used to compare the specified object with this set for equality. Returns true if and only if the specified object is also a set, both sets have the same size, and all corresponding pairs of elements in the two sets are equal. (Two elements e1 a 2 min read LinkedHashSet containsAll() method in Java with Example The containsAll() method of Java LinkedHashSet is used to check whether two sets contain the same elements or not. It takes one set as a parameter and returns True if all of the elements of this set is present in the other set. Syntax: public boolean containsAll(Collection C) Parameters: The paramet 2 min read LinkedHashSet clear() method in Java with Examples The clear() method of java.util.LinkedHashSet class is used to remove all of the elements from this set. The set will be empty after this call returns. Syntax: public void clear() Return Value: This method does not return anything. Below are the examples to illustrate the clear() method. Example 1: 2 min read LinkedHashSet contains() Method in Java with Examples In Java, LinkedHashSet class contains methods known as contains() which is used to return true if this set contains the specified element otherwise false. Syntax: public boolean contains(Object o) Parameters: Element o as a parameter whose presence in this set is to be tested. Return Type: A boolean 2 min read Like