AbstractSet Class in Java with Examples
The abstracts 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.
Class Hierarchy:
java.lang.Object ? java.util.AbstractCollection<E> ? Class AbstractSet<E>
Syntax:
public abstract class AbstractSet<E> extends AbstractCollection<E> implements Set<E> Where E is the type of elements maintained by this Set.
Constructors in Java AbstractSet:
- protected AbstractSet(): The default constructor, but being protected, it doesn’t allow to create an AbstractSet object.
Below is a sample program to illustrate the java AbstractSet:
filter_none
edit
play_arrow
brightness_4
|
Output:
AbstractSet: [1, 2, 3, 4, 5]
Methods in Java AbstractSet:
- 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).
Example:
filter_none
edit
play_arrow
brightness_4
|
Output:
AbstractSet before removeAll() operation : [1, 2, 3, 4, 5] Collection Elements to be removed : [1, 2, 3]
post a comment