Use the retainAll() method of Set:

Set<String> s1;
Set<String> s2;
s1.retainAll(s2); // s1 now contains only elements in both sets

If you want to preserve the sets, create a new set to hold the intersection:

Set<String> intersection = new HashSet<String>(s1); // use the copy constructor
intersection.retainAll(s2);

The javadoc of retainAll() says it's exactly what you want:

Retains only the elements in this set that are contained in the specified collection (optional operation). In other words, removes from this set all of its elements that are not contained in the specified collection. If the specified collection is also a set, this operation effectively modifies this set so that its value is the intersection of the two sets.

Answer from Bohemian on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › find-the-intersection-of-two-hashsets-in-java
Find the Intersection of Two HashSets in Java - GeeksforGeeks
July 23, 2025 - In this method, we can use loops to iterate over a set and check whether it is present in another set, if it is present in another set too then it should be in the intersect HashSet, so add it to the set using add() method.
🌐
Baeldung
baeldung.com › home › java › java collections › set operations in java
Set Operations in Java | Baeldung
May 5, 2026 - First, we’re going to use the retainAll method to create the intersection of our sample sets. Because retainAll modifies the set directly, we’ll make a copy of setA called intersectSet.
🌐
Medium
medium.com › @alxkm › how-to-find-the-intersection-between-two-sets-in-java-4655fde6b4a1
Java Interview: How to Find the Intersection Between Two Sets in Java | by Alex Klimenko | Medium
August 9, 2025 - Java's Set interface (e.g., HashSet, TreeSet) provides built-in methods to compute intersections. ... import java.util.*; public class SetIntersectionExample { public static void main(String[] args) { Set<Integer> setA = new HashSet<>(Arrays.asList(1, 2, 3, 4)); Set<Integer> setB = new HashSet<>(Arrays.asList(3, 4, 5, 6)); setA.retainAll(setB); // modifies setA to keep only elements also in setB System.out.println("Intersection: " + setA); // Output: [3, 4] } }
🌐
Scaler
scaler.com › home › topics › set intersection in java
Set Intersection in Java - Scaler Topics
December 16, 2022 - There are three ways for finding the intersection of two sets: brute force, using the retainAll() method, and using the Guava library.
🌐
Vultr Docs
docs.vultr.com › java › examples › calculate the intersection of two sets
Java Program to Calculate the intersection of two sets
December 17, 2024 - In Java, calculating the intersection of two sets involves finding the common elements between them. This operation is fundamental in many applications like data analysis, where comparing datasets is frequent.
🌐
Programiz
programiz.com › java-programming › examples › calculate-set-intersection
Java Program to Calculate the intersection of two sets
import java.util.HashSet; import java.util.Set; class Main { public static void main(String[] args) { // create first set Set<Integer> primeNumbers = new HashSet<>(); primeNumbers.add(2); primeNumbers.add(3); System.out.println("Prime Numbers: " + primeNumbers); // create second set Set<Integer> evenNumbers = new HashSet<>(); evenNumbers.add(2); evenNumbers.add(4); System.out.println("Even Numbers: " + evenNumbers); // Intersection of two sets evenNumbers.retainAll(primeNumbers); System.out.println("Intersection: " + evenNumbers); } }
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › java › sets-intersection-function-guava-java
Sets intersection() function | Guava | Java - GeeksforGeeks
November 14, 2018 - public static <E> Sets.SetView<E> intersection(Set<E> set1, Set<?> set2) Return Value: This method returns an unmodifiable view of the intersection of two sets. Below examples illustrate the working of Sets intersection method: Example 1: ... // Java code to show implementation // of Guava's Sets.intersection() method import com.google.common.collect.Sets; import java.util.Set; class GFG { // Driver's code public static void main(String[] args) { // Creating first set Set<Integer> set1 = Sets.newHashSet(10, 20, 30, 40, 50); // Creating second set Set<Integer> set2 = Sets.newHashSet(30, 50, 70, 90); // Using Guava's Sets.intersection() method Set<Integer> answer = Sets.intersection(set1, set2); // Displaying the intersection of set1 and set2 System.out.println("Set 1: " + set1); System.out.println("Set 2: " + set2); System.out.println("Set 1 intersection Set 2: " + answer); } }
🌐
Coddy.Tech
coddy.tech › learn › java › math - intersection of hashset
Math - Intersection of HashSet – Java | Coddy
March 8, 2026 - In Java, you can compute the intersection by using the retainAll() method. ... HashSet<Integer> set1 = new HashSet<>(); set1.add(1); set1.add(2); HashSet<Integer> set2 = new HashSet<>(); set2.add(2); set2.add(3);
🌐
Codewithz
codewithz.com › category › java › section › basics › blog › intersection-of-two-sets-in-java
Intersection of Two Sets in Java
July 16, 2020 - Sometimes in our project development work we require to take an intersection of two sets to get the values from both the Set. For that we have the method of Collection class by name retainAll() which help us to take the intersection. ... import java.util.*; public class SetIntersection { public ...
🌐
JavaBeat
javabeat.net › get-intersection-two-sets-in-java
How to Get Intersection of Two Sets in Java
January 22, 2024 - A set that contains common elements of the two given sets is known as the “intersection of two sets”. To calculate the intersection of two sets in Java, you can use different methods such as “brute force”, “retainAll()”, and “Sets.intersection()”. Among all these methods, the ...
🌐
CodeSignal
codesignal.com › learn › courses › mastering-complex-data-structures-in-java › lessons › understanding-sets-in-java
Understanding Sets in Java
For these sets, the intersection is [3, 4]. removeAll: Returns a set containing elements that are in the first set but not in the second set. Here, the result is [1, 2] for set1. Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignalStart learning today! ... import java.util.HashSet; public class Solution { public static void main(String[] args) { HashSet<Integer> mySet = new HashSet<>(); mySet.add(1); mySet.add(2); mySet.add(3); mySet.add(4); mySet.add(5); mySet.add(5); // Adding duplicate System.out.println(mySet); // Output: [1, 2, 3, 4, 5] } }
🌐
Python Examples
pythonexamples.org › java › how-to-find-intersection-of-two-sets
How to find Intersection of Two Sets in Java
To find the intersection of two sets in Java, you can use the retainAll method provided by the Set interface.
🌐
Medium
medium.com › @alxkm › how-to-find-intersection-and-difference-between-two-sets-or-lists-in-java-5a873a8bff5d
Java Interview: How to Find Intersection and Difference Between Two Sets or Lists in Java | by Alex Klimenko | Medium
August 9, 2025 - Set<String> sortedSet1 = new TreeSet<>(list1); Set<String> sortedSet2 = new TreeSet<>(list2); Set<String> sortedIntersection = new TreeSet<>(sortedSet1); sortedIntersection.retainAll(sortedSet2); Set<String> sortedDifference = new TreeSet<>(sortedSet1); sortedDifference.removeAll(sortedSet2); System.out.println("Sorted Intersection: " + sortedIntersection); System.out.println("Sorted Difference: " + sortedDifference); ... Use Set methods (retainAll, removeAll) for performance when order doesn't matter. Use Streams for readable, modern code and when preserving order is important. Use loops for explicit control or in Java versions prior to 8.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › API › Intersection_Observer_API
Intersection Observer API - Web APIs | MDN
September 2, 2026 - Starting at the target's immediate parent block and moving outward, each containing block's clipping (if any) is applied to the intersection rectangle. A block's clipping is determined based on the intersection of the two blocks and the clipping mode (if any) specified by the overflow property. Setting overflow to anything but visible causes clipping to occur.
🌐
Java Guides
javaguides.net › 2024 › 05 › java-find-intersection-of-two-lists.html
Java: Find Intersection of Two Lists
May 29, 2024 - Two lists are created and populated with elements. The retainAll method is used to retain only the elements in list1 that are also in list2. ... The Stream API (introduced in Java 8) provides a modern and concise way to find the intersection of two lists.
🌐
LeetCode
leetcode.com › problems › intersection-of-two-linked-lists
Intersection of Two Linked Lists - LeetCode
- Note that the intersected node's value is not 1 because the nodes with value 1 in A and B (2nd node in A and 3rd node in B) are different node references. In other words, they point to two different locations in memory, while the nodes with value 8 in A and B (3rd node in A and 4th node in B) point to the same location in memory.
🌐
W3Schools
w3schools.com › python › ref_set_intersection.asp
Python Set intersection() Method
Remove List Duplicates Reverse ... x.intersection(y) print(z) Try it Yourself » · The intersection() method returns a set that contains the similarity between two or more sets....
🌐
TutorialsPoint
tutorialspoint.com › article › get-the-intersection-of-two-sets-in-java
Get the intersection of two sets in Java
To get the intersection of two sets, use the retainAll() method. Here are out two set − ... HashSet <String> set2 = new HashSet <String>(); set2.add("Mat"); set2.add("Cat"); set2.add("Fat"); set2.add("Hat"); ... import java.util.*; public ...