AbstractSet Class in Java (original) (raw)

Last Updated : 14 Feb, 2025

In Java, the **AbstractSet class is part of the Java Collections Framework. It provides a Skeleton implementation of the set interface, which is a collection that does not allow duplicate elements. This class is abstract, meaning it cannot be instantiated directly, but it can be extended to create a custom set implementation

**Example: This example demonstrates how to insert elements into a set and print them.

Java `

// Java Program to demonstrates the // working of AbstractSet class import java.util.HashSet; import java.util.Set;

abstract class AbstractSet {

// Abstract method to insert an element
public abstract void insert(int element);

// Abstract method to display the elements
public abstract void display();

}

class MySet extends AbstractSet {

// Set to hold the elements
private Set<Integer> elements;

// Constructor
public MySet() {
    elements = new HashSet<>();
}

// Implement the insert method
public void insert(int element) {
    elements.add(element);
}

// Implement the display method
public void display() {
    for (Integer element : elements) {
        System.out.println(element);
    }
}

}

public class Geeks { public static void main(String[] args) { MySet s = new MySet();

    // Insert elements
    s.insert(10);
    s.insert(20);
    s.insert(30);
    
    // Display elements
    System.out.println("Elements in the set:");
    s.display();
}

}

`

Output

Elements in the set: 20 10 30

AbstractSet Class Hierarchy

From the below diagram, it can be concluded that it implements Iterable, Collection, Set interfaces. The direct subclasses are CopyOnWriteArraySet, EnumSet, HashSet, TreeSet.

AbstractSet-Class-in-Java

Declaration of AbstractSet Class

In Java, the declaration of AbstractSet Class can be done as:

public abstract class AbstractSet extends AbstractCollection implements Set

Constructor

**Constructor **Description
protected AbstractSet() This Constructor is used to prevent direct instantiation of the AbstractSet class, allowing only its subclasses to be instantiated.

**Example: This example demonstrates how a protected constructor in an abstract class can only be called by its subclasses, and how the subclass must implement abstract methods from the abstract class.

Java `

// Java program demonstrating the // working of a protected constructor // in an abstract class

// Abstract class with a protected constructor abstract class AbstractSet {

// Protected constructor, cannot be 
// called directly from outside the class
protected AbstractSet() {
    System.out.println("AbstractSet constructor called");
}

// Abstract method to be implemented by subclasses
public abstract void display();

}

// Concrete subclass extending AbstractSet class MySet extends AbstractSet {

// Constructor for MySet
public MySet() {
    
    // Calls the protected constructor of AbstractSet
    super();
    System.out.println("MySet constructor called");
}

// Implementation of the abstract display method
@Override
public void display() {
    System.out.println("Displaying elements in MySet");
}

}

public class Geeks { public static void main(String[] args) { // We cannot instantiate AbstractSet directly // because it is an abstract class // AbstractSet set = new AbstractSet(); // This would cause a compile-time error

    // Creating an instance of MySet, which is a subclass of AbstractSet
    MySet set = new MySet();
    
    // Calling the display method of MySet
    set.display();
}

}

`

Output

AbstractSet constructor called MySet constructor called Displaying elements in MySet

Performing Various Operations on AbstractSet

**1. Adding elements: We can use add() method to insert element to a AbstractSet class.

**Example: This example demonstrates how to insert elements into an AbstractSet using a LinkedHashSet to maintain the order of insertion and display them in that order.

Java `

// Java Program to Illustrate AbstractSet Class import java.util.*;

// Concrete subclass of AbstractSet public class Geeks { public static void main(String[] args) {

    // Creating a LinkedHashSet to 
    // maintain insertion order
    AbstractSet<Integer> set = new LinkedHashSet<>();

    // Adding elements to the AbstractSet 
    // (through LinkedHashSet)
    set.add(10);
    set.add(20);
    set.add(30);
    set.add(40);
    set.add(50);

    System.out.println("AbstractSet elements: " + set);
}

}

`

Output

AbstractSet elements: [10, 20, 30, 40, 50]

**2. Removing Elements: We can use various methods like remove(), removeAll(), retainAll() and clear() method to remove elements from the AbstractSet.

**Example: This example demonstrates how to remove a single element using remove() and multiple elements using removeAll() from a HashSet, which a concrete subclass of AbstractSet.

Java `

// Java Program to demostrates // the working of remove() and // removeAll() method import java.util.*; public class Geeks { public static void main(String[] args) { // Creating a HashSet, which is a concrete subclass // of AbstractSet Set set = new HashSet<>();

    // Adding elements to the set
    set.add(1);
    set.add(2);
    set.add(3);
    set.add(4);
    set.add(5);

    // Printing the set before removal
    System.out.println("Set before removal(): " + set);

    // Removing a single element using remove()

    // Removes the element 3 from the set
    set.remove(3);
    System.out.println(
        "Set after removal of element 3: " + set);

    // Creating a collection to remove multiple elements
    List<Integer> l = new ArrayList<>();
    l.add(1);
    l.add(5);

    // Removing all elements in the collection using
    // removeAll() Removes all elements that are in l
    set.removeAll(l);
    System.out.println("Set after removeAll() : "
                       + set);
}

}

`

Output

Set before removal(): [1, 2, 3, 4, 5] Set after removal of element 3: [1, 2, 4, 5] Set after removeAll() : [2, 4]

**3. Iterating Elements: We can use the iterator() method to iterate over the elements of a AbstractSet.

**Example:

Java `

// Java Program to demosntrates // the working of iterator() import java.util.*; public class Geeks{ public static void main(String[] args) {

    // Creating a HashSet, which is a 
    // concrete subclass of AbstractSet
    Set<Integer> set = new HashSet<>();
    
    // Adding elements to the set
    set.add(1);
    set.add(2);
    set.add(3);
    set.add(4);
    set.add(5);
    
    // Creating an iterator to iterate over the set
    Iterator<Integer> i = set.iterator();

    // Iterating over the elements using the iterator
    System.out.println("Iterating over the set elements:");
    while (i.hasNext()) {
        
        System.out.print(i.next() + "  ");
    }
}

}

`

Output

Iterating over the set elements: 1 2 3 4 5

Methods

Methods Description
equals​(Object o) Compares the specified object with this set for equality.
hashCode() Returns the hash code value for this set.
removeAll​(Collection<?> c) Removes from this set all of its elements that are contained in the specified collection (optional operation).

**Methods Declared in Class java.util.AbstractCollection

Method Description
add​(E e) Ensures that this collection contains the specified element (optional operation).
addAll​(Collection<? extends E> c) Adds all of the elements in the specified collection to this collection (optional operation).
clear() Removes all of the elements from this collection (optional operation).
contains​(Object o) Returns true if this collection contains the specified element.
containsAll​(Collection<?> c) Returns true if this collection contains all of the elements in the specified collection.
isEmpty() Returns true if this collection contains no elements.
iterator() Returns an iterator over the elements contained in this collection.
remove​(Object o) Removes a single instance of the specified element from this collection, if it is present (optional operation).
retainAll​(Collection<?> c) Retains only the elements in this collection that are contained in the specified collection (optional operation).
toArray() Returns an array containing all of the elements in this collection.
toArray​(T[] a) Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array.
toString() Returns a string representation of this collection.

Methods Declared in Interface java.util.Collection

Method Description
parallelStream() Returns a possibly parallel Stream with this collection as its source.
removeIf​(Predicate<? super E> filter) Removes all of the elements of this collection that satisfy the given predicate.
stream() Returns a sequential Stream with this collection as its source.
toArray​(IntFunction<T[]> generator) Returns an array containing all of the elements in this collection, using the provided generator function to allocate the returned array.

Methods Declared in interface java.lang.Iterable

Method Description
forEach​(Consumer<? super T> action) Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception.

Methods Declared in interface java.util.Set

Method Description
add​(E e) Adds the specified element to this set if it is not already present (optional operation).
addAll​(Collection<? extends E> c) Adds all of the elements in the specified collection to this set if they’re not already present (optional operation).
clear() Removes all of the elements from this set (optional operation).
contains​(Object o) Returns true if this set contains the specified element.
containsAll​(Collection<?> c) Returns true if this set contains all of the elements of the specified collection.
isEmpty() Returns true if this set contains no elements.
iterator() Returns an iterator over the elements in this set.
remove​(Object o) Removes the specified element from this set if it is present (optional operation).
retainAll​(Collection<?> c) Retains only the elements in this set that are contained in the specified collection (optional operation).
size() Returns the number of elements in this set (its cardinality).
spliterator() Creates a Spliterator over the elements in this set.
toArray() Returns an array containing all of the elements in this set.
toArray​(T[] a) Returns an array containing all of the elements in this set; the runtime type of the returned array is that of the specified array

Characteristics

Some of the key characteristics of AbstractSet are listed below: