Set retainAll() Method in Java Last Updated : 11 Jul, 2025 Comments Improve Suggest changes Like Article Like Report In Java, the retainAll() method is used to retain only the elements in a collection that are also present in another collection. It modifies the current collection by removing elements that are not in the specified collection.Example 1: This example demonstrates how the retainAll() method retains only the common elements between two sets. Java // Java Program to demonstrate // the working of retainAll() with Sets import java.util.*; public class Geeks { public static void main(String[] args) { // Create two sets of integers Set<Integer> s1 = new HashSet<>(Arrays.asList(1, 2, 3, 4, 5)); Set<Integer> s2 = new HashSet<>(Arrays.asList(3, 4, 5, 6, 7)); System.out.println("Set 1: " + s1); System.out.println("Set 2: " + s2); // Retain only the elements in // s1 that are also in s2 s1.retainAll(s2); System.out.println( "Modified Set 1 after retainAll: " + s1); } } OutputSet 1: [1, 2, 3, 4, 5] Set 2: [3, 4, 5, 6, 7] Modified Set 1 after retainAll: [3, 4, 5] Syntax of retianAll() Methodboolean retainAll(Collection<?> c)Parameter: This method takes collection as a parameterReturn Type: This method returns "true" if the current collection was modified by retaining common elements otherwise return "false".Example 2: This example demonstrates how the retainAll() method modifies a set by retaining only the common elements with another set, and shows the return value indicating whether the set was modified. Java // Java program to demonstrate how retainAll() // method returns boolean value with Sets import java.util.*; public class Geeks { public static void main(String[] args) { // Create two sets of integers Set<Integer> s1 = new HashSet<>(Arrays.asList(1, 2, 3, 4, 5)); Set<Integer> s2 = new HashSet<>(Arrays.asList(3, 4, 5, 6, 7)); System.out.println("Set 1: " + s1); System.out.println("Set 2: " + s2); // Call retainAll and check the return value boolean b = s1.retainAll(s2); System.out.println( "Set 1 after retainAll (common elements): " + s1); System.out.println("Was the set modified? " + b); // Modify s1 to contain all elements from s2 s1 = new HashSet<>(Arrays.asList(3, 4, 5)); // Now retain elements common between s1 and s2 b = s1.retainAll(s2); System.out.println( "Set 1 after retainAll (common elements): " + s1); System.out.println("Was the set modified? " + b); } } Output: Example 3: This example demonstrates that calling retainAll() with a null collection as a parameter throws a NullPointerException. Java // Java Program to demonstrates retainAll() // method throws NullPointerException import java.util.*; public class Geeks { public static void main(String[] args) { // Create a set with null values Set<Integer> s1 = new HashSet<>(Arrays.asList(1, 2, 3, null)); Set<Integer> s2 = null; s1.retainAll(s2); } } Output: Comment More infoAdvertise with us Next Article Set in Java G gopaldave Follow Improve Article Tags : Java Java-Collections Java-Functions java-set Practice Tags : JavaJava-Collections Similar Reads Set in Java The Set Interface is present in java.util package and extends the Collection interface. It is an unordered collection of objects in which duplicate values cannot be stored. It is an interface that implements the mathematical set. This interface adds a feature that restricts the insertion of duplicat 14 min read Set add() method in Java with Examples The add() method of Set in Java is used to add a specific element into a Set collection. The set add() function adds the element only if the specified element is not already present in the set else the function returns False if the element is already present in the Set. Declaration of add() methodbo 2 min read Set contains() method in Java with Examples The Java.util.Set.contains() method is used to check whether a specific element is present in the Set or not. So basically it is used to check if a Set contains any particular element. Syntax: boolean contains(Object element) Parameters: The parameter element is of the type of Set. This is the eleme 2 min read Set remove() method in Java with Examples The java.util.Set.remove(Object O) method is used to remove a particular element from a Set. Syntax: boolean remove(Object O) Parameters: The parameter O is of the type of element maintained by this Set and specifies the element to be removed from the Set. Return Value: This method returns True if t 1 min read Set addAll() Method in Java In Java, the addAll() method of the Set class is used to add all the elements of a specified collection to the current collection. The elements are added randomly without following any specific order.Example 1: This example demonstrates how to merge two TreeSet using the addAll() method.Java// Java 2 min read Set clear() method in Java with Examples The Java.util.Set.clear() method is used to remove all the elements from a Set. Using the clear() method only clears all the element from the set and not deletes the set. In other words, we can say that the clear() method is used to only empty an existing Set. Syntax: void clear() Parameters: The me 1 min read Set containsAll() Method in Java The containsAll() method of Set in Java is used to check if a collection contains all the elements of a specified collection. This method is part of the Collection interface. Example 1: This example checks if all elements of one set are present in another set and it will return true if they are iden 2 min read Set hashCode() Method in Java In Java, the hashCode() method is defined in the Object class and is used to generate a hash code for objects. It plays a very important role in hash-based collections like HashMap, HashSet, and HashTable. Example 1: This example demonstrates how hashCode() is used to get the hash code of the HashSe 2 min read Set iterator() method in Java with Examples The java.util.Set.iterator() method is used to return an iterator of the same elements as the set. The elements are returned in random order from what present in the set. Syntax: Iterator iterate_value = Set.iterator(); Parameters: The function does not take any parameter. Return Value: The method i 1 min read Set removeAll() Method in Java In Java, the removeAll() method is part of the Collection interface. It is used to remove all elements from a collection that are present in another collection.Example 1: This example demonstrates how the removeAll() method removes all elements from the first set that are also present in the second 2 min read Like