AbstractSet Class in Java with Examples

The 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 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).

Class Hierarchy: 

AbstractSet-Class-in-Java

Declaration: 



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

Where E is the type of elements maintained by this Set.

It implements Iterable<E>, Collection<E>, Set<E> interfaces. The direct subclasses are ConcurrentSkipListSet, CopyOnWriteArraySet, EnumSet, HashSet, TreeSet.

Constructor: protected AbstractSet() – The default constructor, but being protected, it doesn’t allow to create an AbstractSet object.

AbstractSet<E> as = new TreeSet<E>();

Example 1:  AbstractSet is an abstract class, so it should be assigned an instance of its subclasses such as TreeSet, HashSet, or EnumSet

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate AbstractSet
import java.util.*;
  
public class GFG1 {
    public static void main(String[] args) throws Exception
    {
  
        try {
  
            // Creating object of AbstractSet<Integer>
            AbstractSet<Integer>
                abs_set = new TreeSet<Integer>();
  
            // Populating abs_set
            abs_set.add(1);
            abs_set.add(2);
            abs_set.add(3);
            abs_set.add(4);
            abs_set.add(5);
  
            // print abs_set
            System.out.println("AbstractSet: "
                               + abs_set);
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}

chevron_right


Output:

AbstractSet: [1, 2, 3, 4, 5]

Example 2: 
 

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate
// methods of AbstractSet
import java.util.*;
  
public class GFG1 {
    
    public static void main(String[] args) throws Exception
    {
  
        try {
  
            // Creating object of AbstractSet<Integer>
            AbstractSet<Integer> abs_set = new TreeSet<Integer>();
  
            // Populating abs_set
            abs_set.add(1);
            abs_set.add(2);
            abs_set.add(3);
            abs_set.add(4);
            abs_set.add(5);
  
            // print abs_set
            System.out.println("AbstractSet before "
                               + "removeAll() operation : "
                               + abs_set);
  
            // Creating another object of ArrayList<Integer>
            Collection<Integer> arrlist2 = new ArrayList<Integer>();
            arrlist2.add(1);
            arrlist2.add(2);
            arrlist2.add(3);
  
            // print arrlist2
            System.out.println("Collection Elements"
                               + " to be removed : "
                               + arrlist2);
  
            // Removing elements from AbstractSet
            // specified in arrlist2
            // using removeAll() method
            abs_set.removeAll(arrlist2);
  
            // print arrlist1
            System.out.println("AbstractSet after "
                               + "removeAll() operation : "
                               + abs_set);
        }
  
        catch (NullPointerException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}

chevron_right


Output:

AbstractSet before removeAll() operation : [1, 2, 3, 4, 5]
Collection Elements to be removed : [1, 2, 3]
AbstractSet after removeAll() operation : [4, 5]

Methods of AbstractSet

METHOD



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.

Reference: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/AbstractSet.html

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Content Manager at LambdaTest

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.