AbstractSet Class in Java with Examples - GeeksforGeeks

17 Jan, 2022
AbstractSet class in Java is a part of the Java Collection Framework which implements the Collection interface and extends the AbstractCollection class. It provides a skeletal implementation of the Set interface. This class does not override any of the implementations from the AbstractCollection class, but merely adds implementations for the equals() and hashCode() method.
The process of implementing a set by extending this class is identical to that of implementing a Collection by extending AbstractCollection, except that all of the methods and constructors in subclasses of this class must obey the additional constraints imposed by the Set interface (for instance, the add method must not permit the addition of multiple instances of an object to a set).
From the class hierarchy diagram, it can be concluded that it implements Iterable<E>, Collection<E>, Set<E> interfaces. The direct subclasses are ConcurrentSkipListSet, CopyOnWriteArraySet, EnumSet, HashSet, TreeSet. As we already discussed above, AbstractSet is an abstract class, so it should be assigned an instance of its subclasses such as TreeSet, HashSet, or EnumSet.
Syntax: Declaration 
public abstract class AbstractSet<E>
extends AbstractCollection<E>
implements Set<E>
Where E is the type of elements maintained by this Set.
1. protected AbstractSet(): The default constructor, but being protected, it doesn’t allow the creation of an AbstractSet object.
AbstractSet<E> as = new TreeSet<E>();
METHOD
DESCRIPTION
Example 1:
Java
// Java Program to Illustrate AbstractSet Class // Importing required classesimport java.util.*; // Main classpublic class GFG {     // Main driver method    public static void main(String[] args) throws Exception    {        // Try block to check for exceptions        try {             // Creating an empty TreeSet of integer type by            // creating object of AbstractSet            AbstractSet<Integer> abs_set                = new TreeSet<Integer>();             // Populating TreeSet            // using add() method            abs_set.add(1);            abs_set.add(2);            abs_set.add(3);            abs_set.add(4);            abs_set.add(5);             // Printing the elements inside above TreeSet            System.out.println("AbstractSet: " + abs_set);        }         // Catch block to handle the exceptions        catch (Exception e) {             // Display exception on console            System.out.println(e);        }    }}
AbstractSet: [1, 2, 3, 4, 5]
Example 2:
Java
// Java Program to Illustrate Methods// of AbstractSet class // Importing required classesimport java.util.*; // Main classpublic class GFG {     // Main driver method    public static void main(String[] args) throws Exception    {         // Try block to check for exceptions        try {             // Creating an empty TreeSet of integer type            AbstractSet<Integer> abs_set                = new TreeSet<Integer>();             // Populating above TreeSet            // using add() method            abs_set.add(1);            abs_set.add(2);            abs_set.add(3);            abs_set.add(4);            abs_set.add(5);             // Printing the elements inside TreeSet            System.out.println("AbstractSet before "                               + "removeAll() operation : "                               + abs_set);             // Creating an ArrayList of integer type            Collection<Integer> arrlist2                = new ArrayList<Integer>();             // Adding elements to above ArrayList            arrlist2.add(1);            arrlist2.add(2);            arrlist2.add(3);             // Printing the ArrayList elements            System.out.println("Collection Elements"                               + " to be removed : "                               + arrlist2);             // Removing elements from AbstractSet specified            // using removeAll() method            abs_set.removeAll(arrlist2);             // Printing the elements of ArrayList            System.out.println("AbstractSet after "                               + "removeAll() operation : "                               + abs_set);        }         // Catch block to handle the exceptions        catch (NullPointerException e) {             // Display exception on console            System.out.println("Exception thrown : " + e);        }    }}
Output:
Some other methods of classes or interfaces are defined below that somehow helps us to get a better understanding of AbstractSet Class as follows:
METHOD
DESCRIPTION
METHOD
DESCRIPTION
METHOD
DESCRIPTION
METHOD
DESCRIPTION
SachinShinde1
Akanksha_Rai
Ganeshchowdharysadanala
solankimayank
surindertarika1234
anikakapoor
simranarora5sos
Java - util package
Java-AbstractSet
Java-Collections
Java
Java
Java-Collections
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Arrays in Java
Split() String method in Java with examples
For-each loop in Java
Reverse a string in Java
HashMap in Java with Examples
Interfaces in Java
Object Oriented Programming (OOPs) Concept in Java
Arrays.sort() in Java with examples
ArrayList in Java
How to iterate any Map in Java