|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object java.util.AbstractCollection java.util.AbstractSet java.util.HashSet
public class HashSet
This class provides a HashMap-backed implementation of the Set interface.
Most operations are O(1), assuming no hash collisions. In the worst case (where all hashes collide), operations are O(n). Setting the initial capacity too low will force many resizing operations, but setting the initial capacity too high (or loadfactor too low) leads to wasted memory and slower iteration.
HashSet accepts the null key and null values. It is not synchronized,
so if you need multi-threaded access, consider using:
Set s = Collections.synchronizedSet(new HashSet(...));
The iterators are fail-fast, meaning that any structural
modification, except for remove()
called on the iterator
itself, cause the iterator to throw a
ConcurrentModificationException
rather than exhibit
non-deterministic behavior.
Collection
,
Set
,
TreeSet
,
Collections.synchronizedSet(Set)
,
HashMap
,
LinkedHashSet
Constructor Summary | |
---|---|
HashSet()
Construct a new, empty HashSet whose backing HashMap has the default capacity (11) and loadFacor (0.75). |
|
HashSet(Collection c)
Construct a new HashSet with the same elements as are in the supplied collection (eliminating any duplicates, of course). |
|
HashSet(int initialCapacity)
Construct a new, empty HashSet whose backing HashMap has the supplied capacity and the default load factor (0.75). |
|
HashSet(int initialCapacity,
float loadFactor)
Construct a new, empty HashSet whose backing HashMap has the supplied capacity and load factor. |
Method Summary | |
---|---|
boolean |
add(Object o)
Adds the given Object to the set if it is not already in the Set. |
void |
clear()
Empties this Set of all elements; this takes constant time. |
boolean |
contains(Object o)
Returns true if the supplied element is in this Set. |
boolean |
isEmpty()
Returns true if this set has no elements in it. |
Iterator |
iterator()
Returns an Iterator over the elements of this Set, which visits the elements in no particular order. |
boolean |
remove(Object o)
Removes the supplied Object from this Set if it is in the Set. |
int |
size()
Returns the number of elements in this Set (its cardinality). |
Methods inherited from class java.util.AbstractSet |
---|
equals, hashCode, removeAll |
Methods inherited from class java.util.AbstractCollection |
---|
addAll, containsAll, retainAll, toArray, toArray, toString |
Methods inherited from class java.lang.Object |
---|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
Methods inherited from interface java.util.Set |
---|
addAll, containsAll, equals, hashCode, removeAll, retainAll, toArray, toArray |
Constructor Detail |
---|
public HashSet()
public HashSet(int initialCapacity)
initialCapacity
- the initial capacity of the backing HashMap
IllegalArgumentException
- if the capacity is negativepublic HashSet(int initialCapacity, float loadFactor)
initialCapacity
- the initial capacity of the backing HashMaploadFactor
- the load factor of the backing HashMap
IllegalArgumentException
- if either argument is negative, or
if loadFactor is POSITIVE_INFINITY or NaNpublic HashSet(Collection c)
c
- a collection of initial set elements
NullPointerException
- if c is nullMethod Detail |
---|
public boolean add(Object o)
add
in interface Collection
add
in interface Set
add
in class AbstractCollection
o
- the Object to add to this Set
public void clear()
clear
in interface Collection
clear
in interface Set
clear
in class AbstractCollection
Iterator.remove()
public boolean contains(Object o)
contains
in interface Collection
contains
in interface Set
contains
in class AbstractCollection
o
- the Object to look for
public boolean isEmpty()
isEmpty
in interface Collection
isEmpty
in interface Set
isEmpty
in class AbstractCollection
size() == 0
.AbstractCollection.size()
public Iterator iterator()
iterator
in interface Collection
iterator
in interface Set
iterator
in class AbstractCollection
ConcurrentModificationException
public boolean remove(Object o)
remove
in interface Collection
remove
in interface Set
remove
in class AbstractCollection
o
- the object to remove
Iterator.remove()
public int size()
size
in interface Collection
size
in interface Set
size
in class AbstractCollection
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |