public class HashSet<E> extends Set<E>
Constructor and Description |
---|
HashSet()
Constructs a new empty instance of
HashSet . |
HashSet(Collection<E> collection)
Constructs a new instance of
HashSet containing the unique elements in the specified collection. |
HashSet(E... args)
Constructs a HashSet, with the specified initial members.
|
HashSet(int capacity)
Constructs a new instance of
HashSet with the specified capacity. |
HashSet(int capacity,
float loadFactor)
Constructs a new instance of
HashSet with the specified capacity and load factor. |
Modifier and Type | Method and Description |
---|---|
boolean |
add(E object)
Adds the specified object to this
HashSet if not already present. |
void |
clear()
Removes all elements from this
HashSet , leaving it empty. |
boolean |
contains(E object)
Searches this
HashSet for the specified object. |
boolean |
isEmpty()
Returns true if this
HashSet has no elements, false otherwise. |
Iterator<E> |
iterator()
Returns an Iterator on the elements of this
HashSet . |
boolean |
remove(E object)
Removes the specified object from this
HashSet . |
int |
size()
Returns the number of elements in this
HashSet . |
addAll, containsAll, retainAll, toArray, toArray, toString
public HashSet()
HashSet
.public HashSet(int capacity)
HashSet
with the specified capacity.capacity
- the initial capacity of this HashSet
.public HashSet(int capacity, float loadFactor)
HashSet
with the specified capacity and load factor.capacity
- the initial capacity.loadFactor
- the initial load factor.public HashSet(Collection<E> collection)
HashSet
containing the unique elements in the specified collection.
Changes from the java.util version: The constructor there supported covariance, taking a Collection<? extends
E> argument where the collection can be of a subclass of E. This constructor only allows collections of
exact type E, but you can use the addAll method instead if you really need to copy a collection of a subclass.collection
- the collection of elements to addpublic HashSet(E... args)
args
- elements to add initially to the setpublic boolean add(E object)
HashSet
if not already present.add
in class Collection<E>
object
- the object to add.true
when this HashSet
did not already contain the object, false
otherwisepublic void clear()
HashSet
, leaving it empty.clear
in class Collection<E>
isEmpty()
,
size()
public boolean contains(E object)
HashSet
for the specified object.contains
in class Collection<E>
object
- the object to search for.true
if object
is an element of this HashSet
, false
otherwise.public boolean isEmpty()
HashSet
has no elements, false otherwise.isEmpty
in class Collection<E>
true
if this HashSet
has no elements, false
otherwise.size()
public Iterator<E> iterator()
HashSet
.iterator
in interface java.lang.Iterable<E>
iterator
in class Collection<E>
HashSet
.Iterator
public boolean remove(E object)
HashSet
.remove
in class Collection<E>
object
- the object to remove.true
if the object was removed, false
otherwise.public int size()
HashSet
.size
in class Collection<E>
HashSet
.