![]() |
VOOZH | about |
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.
Elements in the set: 20 10 30
From the below diagram, it can be concluded that it implements Iterable<E>, Collection<E>, Set<E> interfaces. The direct subclasses are CopyOnWriteArraySet, EnumSet, HashSet, TreeSet.
In Java, the declaration of AbstractSet Class can be done as:
public abstract class AbstractSet<E> extends AbstractCollection<E> implements Set<E>
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.
AbstractSet constructor called MySet constructor called Displaying elements in MySet
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.
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.
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:
Iterating over the set elements: 1 2 3 4 5
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). |
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. |
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. |
Method | Description |
|---|---|
| Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception. |
Method | Description |
|---|---|
| Adds the specified element to this set if it is not already present (optional operation). | |
| Adds all of the elements in the specified collection to this set if they’re not already present (optional operation). | |
| Removes all of the elements from this set (optional operation). | |
| Returns true if this set contains the specified element. | |
| Returns true if this set contains all of the elements of the specified collection. | |
| Returns true if this set contains no elements. | |
| Returns an iterator over the elements in this set. | |
| Removes the specified element from this set if it is present (optional operation). | |
| Retains only the elements in this set that are contained in the specified collection (optional operation). | |
| Returns the number of elements in this set (its cardinality). | |
| spliterator() | Creates a Spliterator over the elements in this set. |
| Returns an array containing all of the elements in this set. | |
| Returns an array containing all of the elements in this set; the runtime type of the returned array is that of the specified array |
Some of the key characteristics of AbstractSet are listed below: