AbstractSet class

AbstractSet class is part of the collection framework since JDK 1.2. If you are not familiar with the Collection framework and Set interface then you should read the first. The Abstract Set class is the abstract class that provides a skeleton to the Set interface. In this post, we will see the use of AbstractSet class add why it introduced in JDK.

AbstractSet class in java

The AbstractSet class implements the Set interface and extends the AbstractCollection class. It provides implementations to some methods of the Set interface. So that it can minimize the efforts required to implement the Set interface. Let’s see the hierarchy of Abstract Set class.

Abstract set
public abstract class AbstractSet<E> extends AbstractCollection<E> implements Set<E>

The AbstractSet class implements some methods of Set interface and provides a skeletal implementation of the Set interface. It doesn’t override the functionality of the Abstract collection class. Also, It provides body to three methods of the Set interface. In conclusion, It adds the implementation of equal() ,hashCode() and removeAll() method.

The equals() and hashCode() methods are overridden and providing the implementation to check the equality of objects. If you want to know why and when we should override them. You can read it from here.

equals() method in java
hashCode() method in java

equals() method

The equals method Compares the specified object with this set for equality.
Therefore, It performs three operations in the equals method.

  1. It returns true if the specified object and this are the same by comparing them by ==.
  2. Then it compares the size of both objects. If the size is the same then move to the third step.
  3. Every member of the given set is contained in this set

The Set interface has different implementations like HashSet, LinkedHashSet, TreeSet. So, It ensures the equals method works properly with these implementations.

Hence, Let’s see the code of removeAll(Collection c) method.

public boolean removeAll(Collection<?> c) {
        Objects.requireNonNull(c);
        boolean modified = false;

        if (size() > c.size()) {
            for (Object e : c)
                modified |= remove(e);
        } else {
            for (Iterator<?> i = iterator(); i.hasNext(); ) {
                if (c.contains(i.next())) {
                    i.remove();
                    modified = true;
                }
            }
        }
        return modified;
    }

Let’s see the example of AbstractSet class.

import java.util.*;

public class AbstractSetClassExample
{
    public static void main(String[] args)
    {
        // Create a HashSet
        AbstractSet<String> names = new HashSet<String>();

        names.add("Ram");
        names.add("Sham");
        names.add("Krisha");
        names.add("Vindhu");
        names.add("Ravi");

        for ( String name: names)
        {
            System.out.println("Name from HashSet: "+name);
        }
    }
}

Output: Name from HashSet: Ravi
Name from HashSet: Sham
Name from HashSet: Krisha
Name from HashSet: Vindhu
Name from HashSet: Ram

Leave a Comment