|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectjava.util.AbstractCollection<E>
java.util.AbstractSet<T>
java.util.HashSet<T>
public class HashSet<T>
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,
Serialized Form| Constructor Summary | |
|---|---|
HashSet()
Construct a new, empty HashSet whose backing HashMap has the default capacity (11) and loadFacor (0.75). |
|
HashSet(Collection<? extends T> 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(T 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. |
Object |
clone()
Returns a shallow copy of this Set. |
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<T> |
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 |
|---|
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 negative
public 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<? extends T> c)
c - a collection of initial set elements
NullPointerException - if c is null| Method Detail |
|---|
public boolean add(T o)
add in interface Collection<T>add in interface Set<T>add in class AbstractCollection<T>o - the Object to add to this Set
public void clear()
clear in interface Collection<T>clear in interface Set<T>clear in class AbstractCollection<T>Iterator.remove()public Object clone()
clone in class ObjectCloneablepublic boolean contains(Object o)
contains in interface Collection<T>contains in interface Set<T>contains in class AbstractCollection<T>o - the Object to look for
public boolean isEmpty()
isEmpty in interface Collection<T>isEmpty in interface Set<T>isEmpty in class AbstractCollection<T>size() == 0.AbstractCollection.size()public Iterator<T> iterator()
iterator in interface Iterable<T>iterator in interface Collection<T>iterator in interface Set<T>iterator in class AbstractCollection<T>ConcurrentModificationExceptionpublic boolean remove(Object o)
remove in interface Collection<T>remove in interface Set<T>remove in class AbstractCollection<T>o - the object to remove
Iterator.remove()public int size()
size in interface Collection<T>size in interface Set<T>size in class AbstractCollection<T>
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||