AbstractSet
public abstract class AbstractSet
extends AbstractCollection<E>
implements Set<E>
Known indirect subclasses LinkedHashSet<E> | Hash table and linked list implementation of the Set interface, with well-defined encounter order. | |
This class provides a skeletal implementation of the Set
interface to minimize the effort required to implement this interface.
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 addition of multiple instances of an object to a set).
Note that this class does not override any of the implementations from the AbstractCollection
class. It merely adds implementations for equals
and hashCode
.
This class is a member of the Java Collections Framework.
Summary
Public methods |
boolean | equals(Object o) Compares the specified object with this set for equality. |
int | hashCode() Returns the hash code value for this set. |
boolean | removeAll(Collection<?> c) Removes from this set all of its elements that are contained in the specified collection (optional operation). |
Inherited methods |
From class java.util.AbstractCollection boolean | add(E e) Ensures that this collection contains the specified element (optional operation). | boolean | addAll(Collection<? extends E> c) Adds all of the elements in the specified collection to this collection (optional operation). | void | clear() Removes all of the elements from this collection (optional operation). | boolean | contains(Object o) Returns true if this collection contains the specified element. | boolean | containsAll(Collection<?> c) Returns true if this collection contains all of the elements in the specified collection. | boolean | isEmpty() Returns true if this collection contains no elements. | abstract Iterator<E> | iterator() Returns an iterator over the elements contained in this collection. | boolean | remove(Object o) Removes a single instance of the specified element from this collection, if it is present (optional operation). | boolean | removeAll(Collection<?> c) Removes all of this collection's elements that are also contained in the specified collection (optional operation). | boolean | retainAll(Collection<?> c) Retains only the elements in this collection that are contained in the specified collection (optional operation). | abstract int | size() Returns the number of elements in this collection. | <T> T[] | 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. | Object[] | toArray() Returns an array containing all of the elements in this collection. | String | toString() Returns a string representation of this collection. | |
From class java.lang.Object Object | clone() Creates and returns a copy of this object. | boolean | equals(Object obj) Indicates whether some other object is "equal to" this one. | void | finalize() Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. | final Class<?> | getClass() Returns the runtime class of this Object . | int | hashCode() Returns a hash code value for the object. | final void | notify() Wakes up a single thread that is waiting on this object's monitor. | final void | notifyAll() Wakes up all threads that are waiting on this object's monitor. | String | toString() Returns a string representation of the object. | final void | wait(long timeoutMillis, int nanos) Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed. | final void | wait(long timeoutMillis) Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed. | final void | wait() Causes the current thread to wait until it is awakened, typically by being notified or interrupted. | |
From interface java.util.Collection abstract boolean | add(E e) Ensures that this collection contains the specified element (optional operation). | abstract boolean | addAll(Collection<? extends E> c) Adds all of the elements in the specified collection to this collection (optional operation). | abstract void | clear() Removes all of the elements from this collection (optional operation). | abstract boolean | contains(Object o) Returns true if this collection contains the specified element. | abstract boolean | containsAll(Collection<?> c) Returns true if this collection contains all of the elements in the specified collection. | abstract boolean | equals(Object o) Compares the specified object with this collection for equality. | abstract int | hashCode() Returns the hash code value for this collection. | abstract boolean | isEmpty() Returns true if this collection contains no elements. | abstract Iterator<E> | iterator() Returns an iterator over the elements in this collection. | default Stream<E> | parallelStream() Returns a possibly parallel Stream with this collection as its source. | abstract boolean | remove(Object o) Removes a single instance of the specified element from this collection, if it is present (optional operation). | abstract boolean | removeAll(Collection<?> c) Removes all of this collection's elements that are also contained in the specified collection (optional operation). | default boolean | removeIf(Predicate<? super E> filter) Removes all of the elements of this collection that satisfy the given predicate. | abstract boolean | retainAll(Collection<?> c) Retains only the elements in this collection that are contained in the specified collection (optional operation). | abstract int | size() Returns the number of elements in this collection. | default Spliterator<E> | spliterator() Creates a Spliterator over the elements in this collection. | default Stream<E> | stream() Returns a sequential Stream with this collection as its source. | abstract <T> T[] | 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. | abstract Object[] | toArray() Returns an array containing all of the elements in this collection. | default <T> T[] | 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. | |
From interface java.util.Set abstract boolean | add(E e) Adds the specified element to this set if it is not already present (optional operation). | abstract boolean | 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). | abstract void | clear() Removes all of the elements from this set (optional operation). | abstract boolean | contains(Object o) Returns true if this set contains the specified element. | abstract boolean | containsAll(Collection<?> c) Returns true if this set contains all of the elements of the specified collection. | static <E> Set<E> | copyOf(Collection<? extends E> coll) Returns an unmodifiable Set containing the elements of the given Collection. | abstract boolean | equals(Object o) Compares the specified object with this set for equality. | abstract int | hashCode() Returns the hash code value for this set. | abstract boolean | isEmpty() Returns true if this set contains no elements. | abstract Iterator<E> | iterator() Returns an iterator over the elements in this set. | static <E> Set<E> | of(E e1, E e2, E e3) Returns an unmodifiable set containing three elements. | static <E> Set<E> | of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9) Returns an unmodifiable set containing nine elements. | static <E> Set<E> | of(E e1, E e2, E e3, E e4, E e5) Returns an unmodifiable set containing five elements. | static <E> Set<E> | of(E e1, E e2, E e3, E e4, E e5, E e6) Returns an unmodifiable set containing six elements. | static <E> Set<E> | of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8) Returns an unmodifiable set containing eight elements. | static <E> Set<E> | of(E... elements) Returns an unmodifiable set containing an arbitrary number of elements. | static <E> Set<E> | of() Returns an unmodifiable set containing zero elements. | static <E> Set<E> | of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9, E e10) Returns an unmodifiable set containing ten elements. | static <E> Set<E> | of(E e1, E e2) Returns an unmodifiable set containing two elements. | static <E> Set<E> | of(E e1, E e2, E e3, E e4, E e5, E e6, E e7) Returns an unmodifiable set containing seven elements. | static <E> Set<E> | of(E e1, E e2, E e3, E e4) Returns an unmodifiable set containing four elements. | static <E> Set<E> | of(E e1) Returns an unmodifiable set containing one element. | abstract boolean | remove(Object o) Removes the specified element from this set if it is present (optional operation). | abstract boolean | removeAll(Collection<?> c) Removes from this set all of its elements that are contained in the specified collection (optional operation). | abstract boolean | retainAll(Collection<?> c) Retains only the elements in this set that are contained in the specified collection (optional operation). | abstract int | size() Returns the number of elements in this set (its cardinality). | default Spliterator<E> | spliterator() Creates a Spliterator over the elements in this set. | abstract Object[] | toArray() Returns an array containing all of the elements in this set. | abstract <T> T[] | 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. | |
|
Protected constructors
AbstractSet
protected AbstractSet ()
Sole constructor. (For invocation by subclass constructors, typically implicit.)
Public methods
equals
public boolean equals (Object o)
Compares the specified object with this set for equality. Returns true
if the given object is also a set, the two sets have the same size, and every member of the given set is contained in this set. This ensures that the equals
method works properly across different implementations of the Set
interface.
This implementation first checks if the specified object is this set; if so it returns true
. Then, it checks if the specified object is a set whose size is identical to the size of this set; if not, it returns false. If so, it returns containsAll((Collection) o)
.
Parameters |
o | Object : object to be compared for equality with this set |
Returns |
boolean | true if the specified object is equal to this set |
hashCode
public int hashCode ()
Returns the hash code value for this set. The hash code of a set is defined to be the sum of the hash codes of the elements in the set, where the hash code of a null
element is defined to be zero. This ensures that s1.equals(s2)
implies that s1.hashCode()==s2.hashCode()
for any two sets s1
and s2
, as required by the general contract of Object.hashCode
.
This implementation iterates over the set, calling the hashCode
method on each element in the set, and adding up the results.
Returns |
int | the hash code value for this set |
removeAll
public boolean removeAll (Collection<?> c)
Removes from this set all of its elements that are contained in the specified collection (optional operation). If the specified collection is also a set, this operation effectively modifies this set so that its value is the asymmetric set difference of the two sets.
This implementation determines which is the smaller of this set and the specified collection, by invoking the size
method on each. If this set has fewer elements, then the implementation iterates over this set, checking each element returned by the iterator in turn to see if it is contained in the specified collection. If it is so contained, it is removed from this set with the iterator's remove
method. If the specified collection has fewer elements, then the implementation iterates over the specified collection, removing from this set each element returned by the iterator, using this set's remove
method.
Note that this implementation will throw an UnsupportedOperationException
if the iterator returned by the iterator
method does not implement the remove
method.
Parameters |
c | Collection : collection containing elements to be removed from this set |
Returns |
boolean | true if this set changed as a result of the call |