public class HashSet extends AbstractSet implements Set, Cloneable, Serializable
Constructor and Description |
---|
HashSet()
Constructs a new empty instance of
HashSet . |
HashSet(Collection collection)
Constructs a new instance of
HashSet containing the unique
elements in the specified collection. |
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(Object object)
Adds the specified object to this
HashSet if not already present. |
void |
clear()
Removes all elements from this
HashSet , leaving it empty. |
Object |
clone()
Returns a new
HashSet with the same elements and size as this
HashSet . |
boolean |
contains(Object object)
Searches this
HashSet for the specified object. |
boolean |
isEmpty()
Returns true if this
HashSet has no elements, false otherwise. |
Iterator |
iterator()
Returns an Iterator on the elements of this
HashSet . |
boolean |
remove(Object object)
Removes the specified object from this
HashSet . |
int |
size()
Returns the number of elements in this
HashSet . |
equals, hashCode, removeAll
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 collection)
HashSet
containing the unique
elements in the specified collection.collection
- the collection of elements to add.public boolean add(Object object)
HashSet
if not already present.add
in interface Collection
add
in interface Set
add
in class AbstractCollection
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 interface Collection
clear
in interface Set
clear
in class AbstractCollection
isEmpty()
,
size()
public Object clone()
HashSet
with the same elements and size as this
HashSet
.public boolean contains(Object object)
HashSet
for the specified object.contains
in interface Collection
contains
in interface Set
contains
in class AbstractCollection
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 interface Collection
isEmpty
in interface Set
isEmpty
in class AbstractCollection
true
if this HashSet
has no elements,
false
otherwise.size()
public Iterator iterator()
HashSet
.iterator
in interface Collection
iterator
in interface Set
iterator
in class AbstractCollection
HashSet
.Iterator
public boolean remove(Object object)
HashSet
.remove
in interface Collection
remove
in interface Set
remove
in class AbstractCollection
object
- the object to remove.true
if the object was removed, false
otherwise.public int size()
HashSet
.size
in interface Collection
size
in interface Set
size
in class AbstractCollection
HashSet
.