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 an object in a HashSet. You can do by removing that object and adding the replacement one

Let's walk through the following examples to explore in 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 satisfies 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

You 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


Share to social

Van N.

Van N. is a software engineer, creator of HelloKoding. He loves coding, blogging, and traveling. You may find him on GitHub and LinkedIn