Java HashSet.retainAll()

Java HashSet.retainAll() is a method in the Java HashSet class that retains only the elements in the set that are contained in the specified collection. This method modifies the set to contain the intersection of the current set and the specified collection. Introduced in Java 1995 with Java 1.2, retainAll() is useful for filtering sets based on shared elements with another collection.

https://docs.oracle.com/javase/8/docs/api/java/util/HashSet.html