You can remove one or multiple objects in a HashSet by using remove, removeIf, retainAll and removeAll methods

Java does not have a direct method to update objects in a HashSet. Since a Set has only contain one instance of an object, so we can update it by removing it and adding the replacement object

Let's walk through the following examples to find more details

Remove one object with remove(Object)

  • The remove(Object) method removes the specified object from the set if it is present and returns true
@Test
public void removeOneObject() {  
    Set<Integer> set = new HashSet<>();
    set.add(3);
    set.add(1);
    set.add(2);

    boolean result = set.remove(1);
    assertThat(result).isTrue();

    assertThat(set).hasSize(2);
}
  • The remove(Object) method does nothing and returns false if the specified object is not present in the set
@Test
public void removeNonExistingObject() {  
    Set<Integer> set = new HashSet<>();
    set.add(3);
    set.add(1);
    set.add(2);

    boolean result = set.remove(4);
    assertThat(result).isFalse();

    assertThat(set).hasSize(3);
}

Remove multiple objects with removeIf, retainAll, removeAll and clear

  • The removeIf(Predicate) method removes all elements from a set that satisfy the given predicate filter, returns true if any elements were removed, available since Java 8+
@Test
public void removeIf() {  
    Set<Integer> set = new HashSet<>();
    set.add(3);
    set.add(1);
    set.add(2);

    boolean result = set.removeIf(e -> e > 3);
    assertThat(result).isFalse();
    assertThat(set).hasSize(3);

    result = set.removeIf(e -> e > 1);
    assertThat(result).isTrue();
    assertThat(set).hasSize(1);
}
  • The removeAll(Collection) method removes all elements that are contained in the specified collection, returns true if any elements were removed
@Test
public void removeAll() {  
    Set<Integer> set = new HashSet<>();
    set.add(3);
    set.add(1);
    set.add(2);

    boolean result = set.removeAll(Set.of(4, 5));
    assertThat(result).isFalse();
    assertThat(set).hasSize(3);

    result = set.removeAll(Set.of(2, 4));
    assertThat(result).isTrue();
    assertThat(set).hasSize(2);
}
  • The retainAll(Collection) method retains all elements that are contained in the specified collection, returns true if any elements were removed
@Test
public void retainAll() {  
    Set<Integer> set = new HashSet<>();
    set.add(3);
    set.add(1);
    set.add(2);

    boolean result = set.retainAll(Set.of(1, 2, 3));
    assertThat(result).isFalse();
    assertThat(set).hasSize(3);

    result = set.retainAll(Set.of(2, 4, 5));
    assertThat(result).isTrue();
    assertThat(set).hasSize(1);
}
  • The clear() method removes all elements, the set will be empty after this call returns
@Test
public void clear() {  
    Set<Integer> set = new HashSet<>();
    set.add(3);
    set.add(1);
    set.add(2);

    set.clear();
    assertThat(set).hasSize(0);
}

Update an object

We can update an object by removing it and adding the replacement object

@Test
public void updateAnObject() {  
    Set<Integer> set = new HashSet<>();
    set.add(3);
    set.add(1);
    set.add(2);

    set.remove(2);
    set.add(4);

    assertThat(set).hasSize(3);
}


Conclusion

In this tutorial, we learned some ways to update and remove objects in a HashSet. You can find the full source code as below