Known Indirect Subclasses
LinkedHashSet<E> |
Hash table and linked list implementation of the Set interface,
with predictable iteration 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.
Protected Constructor Summary
Public Method Summary
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 Method Summary
From class
java.util.AbstractCollection
boolean
|
add(E e)
Ensures that this collection contains the specified element (optional
operation).
This implementation always throws an
UnsupportedOperationException.
|
boolean
|
addAll( Collection<? extends E> c)
Adds all of the elements in the specified collection to this collection
(optional operation).
This implementation iterates over the specified collection, and adds
each object returned by the iterator to this collection, in turn.
|
void
|
clear()
Removes all of the elements from this collection (optional operation).
This implementation iterates over this collection, removing each
element using the Iterator.remove operation.
|
boolean
|
contains( Object o)
Returns true if this collection contains the specified element.
This implementation iterates over the elements in the collection,
checking each element in turn for equality with the specified element.
|
boolean
|
containsAll( Collection<?> c)
Returns true if this collection contains all of the elements
in the specified collection.
This implementation iterates over the specified collection,
checking each element returned by the iterator in turn to see
if it's contained in this collection.
|
boolean
|
isEmpty()
Returns true if this collection contains no elements.
This implementation returns size() == 0.
|
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).
This implementation iterates over the collection looking for the
specified element.
|
boolean
|
removeAll( Collection<?> c)
Removes all of this collection's elements that are also contained in the
specified collection (optional operation).
This implementation iterates over this collection, checking each
element returned by the iterator in turn to see if it's contained
in the specified collection.
|
boolean
|
retainAll( Collection<?> c)
Retains only the elements in this collection that are contained in the
specified collection (optional operation).
This implementation iterates over this collection, checking each
element returned by the iterator in turn to see if it's contained
in the specified collection.
|
abstract
int
|
size()
Returns the number of elements in this collection.
|
<T>
T[]
|
toArray(T[] contents)
Returns an array containing all of the elements in this collection;
the runtime type of the returned array is that of the specified array.
This implementation returns an array containing all the elements
returned by this collection's iterator in the same order, stored in
consecutive elements of the array, starting with index 0 .
|
Object[]
|
toArray()
Returns an array containing all of the elements in this collection.
This implementation returns an array containing all the elements
returned by this collection's iterator, in the same order, stored in
consecutive elements of the array, starting with index 0 .
|
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)
Compares this instance with the specified object and indicates if they
are equal.
|
void
|
finalize()
Invoked when the garbage collector has detected that this instance is no longer reachable.
|
final
Class<?>
|
getClass()
Returns the unique instance of Class that represents this
object's class.
|
int
|
hashCode()
Returns an integer hash code for this object.
|
final
void
|
notify()
Causes a thread which is waiting on this object's monitor (by means of
calling one of the wait() methods) to be woken up.
|
final
void
|
notifyAll()
Causes all threads which are waiting on this object's monitor (by means
of calling one of the wait() methods) to be woken up.
|
String
|
toString()
Returns a string containing a concise, human-readable description of this
object.
|
final
void
|
wait(long timeout, int nanos)
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object or until the
specified timeout expires.
|
final
void
|
wait(long timeout)
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object or until the
specified timeout expires.
|
final
void
|
wait()
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object.
|
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.
|
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).
|
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.
|
Spliterator<E>
|
|
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.
|
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.
|
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.
|
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).
|
Spliterator<E>
|
spliterator()
Creates a Spliterator over 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.
|
abstract
Object[]
|
toArray()
Returns an array containing all of the elements in this set.
|
Protected Constructors
protected
AbstractSet
()
Sole constructor. (For invocation by subclass constructors, typically
implicit.)
Public Methods
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 to be compared for equality with this set |
Returns
- true if the specified object is equal to this set
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
- the hash code value for this set
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 containing elements to be removed from this set |
Returns
- true if this set changed as a result of the call