Explore the performance of removeAll() in HashSet.

1. Overview

HashSet is a collection for storing unique elements.

In this tutorial, we’ll discuss the performance of the removeAll() method in the _java.util.HashSet _class.

2. HashSet.removeAll()

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>();
    collection.add(1);
    collection.add(3);

    set.removeAll(collection);

    Integer[] actualElements = new Integer[set.size()];
    Integer[] expectedElements = new Integer[] { 2, 4 };
    assertArrayEquals(expectedElements, set.toArray(actualElements));

As a result, elements 1 and 3 will be removed from the set.

3. Internal Implementation and Time Complexity

The removeAll() method determines which one is smaller – the set or the collection. This is done by invoking the _size() _method on the set and the collection.

If the collection has fewer elements than the set, then it iterates over the specified collection with the time complexity O(n). It also checks if the element is present in the set with the time complexity O(1). And if the element is present, it’s being removed from the set using the remove() method of the set, which again has a time complexity of O(1). So the overall time complexity is O(n).

If the set has fewer elements than the collection, then it iterates over this set using O(n). Then it checks if each element is present in the collection by invoking its contains() method. And if such an element is present, then the element is removed from the set. So this depends on the time complexity of the contains() method.

Now in this case, if the collection is an ArrayList, the time complexity of the contains() method is O(m). So overall time complexity to remove all elements present in the ArrayList from the set is O(n * m).

If the collection is again HashSet, the time complexity of the contains() method is O(1). So overall time complexity to remove all elements present in the HashSet from the set is O(n).

#java #programming #developer

Performance of removeAll() in a HashSet
4.00 GEEK