Yes, it calls equals(). compareTo() could only be used if the Set knew that it contained Comparable objects (sorted sets, for instance, might possibly do this).

Answer from Michael Myers on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › set-removeall-method-in-java-with-examples
Set removeAll() Method in Java - GeeksforGeeks
July 11, 2025 - Example 2: This example shows how removeAll() returns a boolean indicating whether any elements were removed from the first set by comparing it to the second set. ... // Java program to demosntrates that // removeAll() returns boolean value import java.util.HashSet; import java.util.Set; public class Geeks { public static void main(String[] args) { Set<String> s1 = new HashSet<>(); s1.add("Geek1"); s1.add("Geek2"); s1.add("Geek3"); Set<String> s2 = new HashSet<>(); s2.add("Geek2"); s2.add("Geek3"); // Removing elements that // exist in s2 from s1 boolean res1 = s1.removeAll(s2); System.out.println("Set1 after removing Set2 elements from it: " + s1); System.out.println("Was the collection modified?
🌐
Baeldung
baeldung.com › home › java › java collections › performance of removeall() in a hashset
Performance of removeAll() in a HashSet | Baeldung
January 8, 2024 - The removeAll method removes all the elements, that are contained in the collection: Set<Integer> set = new HashSet<Integer>(); set.add(1); set.add(2); set.add(3); set.add(4); Collection<Integer> collection = new ArrayList<Integer>(); ...
🌐
Educative
educative.io › answers › what-is-the-setremoveall-method-in-java
What is the Set.removeAll() method in Java?
Line 26: We call the removeAll() function to remove the collection set2 from set1.
🌐
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › java.base › java › util › Set.html
Set (Java SE 11 & JDK 11 )
January 20, 2026 - If the specified collection is also a set, this operation effectively modifies this set so that its value is the asymmetric set difference of the two sets. Specified by: removeAll in interface Collection<E> Parameters: c - collection containing elements to be removed from this set ·
🌐
Scaler
scaler.com › home › topics › removeall() in java
removeAll() Method in Java - Scaler Topics
May 5, 2024 - The removeAll() in Java determines which is smaller—the set or the collection.
Top answer
1 of 3
2
 AtomicReference a = new AtomicReference<>("A");
 AtomicReference a2 = new AtomicReference<>("A");
 System.out.println(a==a2);

your answer lies within.

if instead of Object you use a custom class and you Override equals method it will work as expected.

to get this to work

class AtomicString{
private AtomicReference<String> s;

public AtomicString(String s) {
    this.s = new AtomicReference<>(s);
}

@Override public boolean equals(Object o) {
    if (this == o)
        return true;
    if (o == null || getClass() != o.getClass())
        return false;
    AtomicString that = (AtomicString) o;
    return this.s.get().equals(that.getS().get());
}

public AtomicReference<String> getS() {
    return s;
}

@Override public int hashCode() {
    return Objects.hash(s.get());
}
2 of 3
1

The problem really is the inconsistent "equality" logic. Both TreeSet and HashSet inherit AbstractSet#removeAll, which iterates over the smaller set, therefore using that set's object comparison. As it turns out, "equality" logic can be overridden using a TreeSet.

This is a problem that you can avoid by choosing one of the two Set implementations. If you choose TreeSet, then you must also use the same comparator.

You can't really use HashSet in this case because AtomicReference doesn't have an implementation of equals/hashCode that would work for you. So your only practical choice is to use TreeSet:

Comparator<Object> comparator = (a, b) -> a.toString().compareTo(b.toString());
final Set<Object> set1 = new TreeSet<>(comparator);
final Set<Object> set2 = new TreeSet<>(comparator);

This will break your current tests, but because elements are now being removed as they should (according to your comparator's logic).

Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › java › set-remove-method-in-java-with-examples
Set remove() method in Java with Examples - GeeksforGeeks
December 26, 2025 - The java.util.Set.remove(Object o) method is used to remove a specific element from a Set. It returns true if the element was present and successfully removed, otherwise, it returns false.
🌐
OpenRewrite
docs.openrewrite.org › recipe catalog › static analysis and remediation › replace `java.util.set#removeall(java.util.collection)` with `java.util.collection#foreach(set::remove)`
Replace `java.util.Set#removeAll(java.util.Collection)` with `java.util.Collection#forEach(Set::remove)` | OpenRewrite Docs
import java.util.Collection; import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Set; class T { static { Set<String> s = new HashSet<>(); List<String> l = Collections.singletonList("a"); l.forEach(s::remove); } Set<String> removeFromSet(Set<String> s, Collection<String> c) { c.forEach(s::remove); return s; } } @@ -11,1 +11,1 @@ Set<String> s = new HashSet<>(); List<String> l = Collections.singletonList("a"); - s.removeAll(l); + l.forEach(s::remove); } @@ -15,1 +15,1 @@ Set<String> removeFromSet(Set<String> s, Collection<String> c) { - s.removeAll(c); + c.forEach(s::remove); return s;
Published: 1 week ago
🌐
GeeksforGeeks
geeksforgeeks.org › java › abstractset-removeall-method-in-java-with-examples
AbstractSet removeAll() Method in Java - GeeksforGeeks
July 11, 2025 - The removeAll() method of the Java AbstractSet class is part of the Java Collections Framework. This method is used to remove all elements from the current set that are also contained in another collection or set.
🌐
Jsparrow
jsparrow.github.io › rules › replace-set-remove-all-with-for-each.html
Replace Set.removeAll With ForEach | jSparrow Documentation
Calling the method 'removeAll' on a Set with a List as invocation argument may lead to performance problems due to a possible O(n^2) complexity. This rule replaces such invocations. For example, the invocation 'mySet.removeAll(myList);' is replaced by 'myList.forEach(mySet::remove);'.
🌐
JetBrains
jetbrains.com › help › inspectopedia › SlowAbstractSetRemoveAll.html
Call to 'set.removeAll(list)' may work slowly | Inspectopedia Documentation
Reports calls to java.util.Set.removeAll() with a java.util.List argument. Such a call can be slow when the size of the argument is greater than or equal to the size of the set, and the set is a subclass of java.util.AbstractSet. In this case, List.contains() is called for each element in the set, which will perform a linear search.
🌐
GeeksforGeeks
geeksforgeeks.org › java › hashset-removeall-method-in-java-with-example
HashSet removeAll() method in Java with Example - GeeksforGeeks
December 9, 2021 - The removeAll() method of java.util.HashSet 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 ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › sortedset-removeall-method-in-java-with-examples
SortedSet removeAll() method in Java with Examples - GeeksforGeeks
July 12, 2025 - The removeAll() method of SortedSet interface is used to remove from this SortedSet all of its elements that are contained in the specified collection. Syntax: ... Parameters: This method takes collection c as a parameter containing elements ...
🌐
Code Pumpkin
codepumpkin.com › home › performance issue with hashset removeall() method
Performance Issue with HashSet RemoveAll() method | Code Pumpkin
October 22, 2018 - However, Java9 has introduced Convenience Factory Methods for Collections to initialize Set, List or Map as below · List<String> list = List.of("a", "b", "c"); Set<String> set = Set.of("a", "b", "c"); Map<String, Integer> cities = Map.of("a", 1, "b", 2,"c",3); public boolean removeAll(Collection c) { Objects.requireNonNull(c); boolean modified = false; if (size() > c.size()) { for (Iterator i = c.iterator(); i.hasNext(); ) modified |= remove(i.next()); } else { for (Iterator i = iterator(); i.hasNext(); ) { if (c.contains(i.next())) { i.remove(); modified = true; } } } return modified; } So, How it is implemented internally?
🌐
Reddit
reddit.com › r/java › the hashset.removeall method is surprisingly slow (in some cases)
r/java on Reddit: The HashSet<T>.removeAll method is surprisingly slow (in some cases)
March 11, 2020 - Otherwise the definition of Set would be too constricted, which is against the idea of interfaces. ... Read the other comment dude. ... Calling u/s888marks. This is your area of expertise isn't it? ... He's already aware of this (https://bugs.openjdk.java.net/browse/JDK-6982173). This is 5 years old, not sure why it resurfaced. ... And that bug is 10 years old. Basically, fixing the issue would change documented behaviour. Rock and hard place. ... public boolean removeAll(Collection<?> c) { Objects.requireNonNull(c); boolean modified = false; if (size() > c.size()) { for (Iterator<?> i = c.iterator(); i.hasNext(); ) modified |= remove(i.next()); } else { for (Iterator<?> i = iterator(); i.hasNext(); ) { if (c.contains(i.next())) { i.remove(); modified = true; } } } return modified; }
🌐
TutorialsPoint
tutorialspoint.com › java-program-to-remove-all-elements-from-a-set-in-java
Remove elements from a Set
August 1, 2024 - You can remove an element from a set using this method. import java.util.HashSet; import java.util.Set; public class RemovingElements { public static void main(String args[]) { Set set = new HashSet(); set.add(100); set.add(501); set.add(302); set.add(420); System.out.println("Contents of the set are: "+set); set.remove(100); System.out.println("Contents of the set after removing one element : "+set); } }
🌐
CodeGym
codegym.cc › java blog › java collections › arraylist removeall() method in java
ArrayList removeAll() method in Java
January 7, 2025 - The ArrayList removeAll() method in Java doesn’t remove all elements from proper ArrayList, as its name might suggest. If you need this operation, use the ArrayList clear() method. Unlike clear(), the removeAll() method removes all items from ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › treeset-removeall-method-in-java-with-example
TreeSet removeAll() method in Java with Example - GeeksforGeeks
October 4, 2019 - The removeAll() method of java.util.TreeSet 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 ...
🌐
Programiz
programiz.com › java-programming › library › arraylist › removeall
Java ArrayList removeAll()
Java ArrayList removeAll() Java ArrayList remove() Java ArrayList size() Java ArrayList isEmpty() Java ArrayList subList() Java ArrayList set() Java ArrayList sort() Java ArrayList toArray() Java ArrayList toString() Java ArrayList ensureCapacity() Java ArrayList lastIndexOf() Java ArrayList retainAll() Java ArrayList containsAll() Java ArrayList trimToSize() Java ArrayList removeRange() Java ArrayList replaceAll() Java ArrayList removeIf() Java ArrayList foreach() Java ArrayList iterator() Java ArrayList retainAll() Java ArrayList containsAll() Java ArrayList addAll() Java ArrayList clear() Java ArrayList set() Java ArrayList removeRange() The syntax of the removeAll() method is: arraylist.removeAll(Collection c); Here, arraylist is an object of the ArrayList class.