public class LongHashSet extends AbstractLongSet implements LongSet
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.
LongCollection
,
LongSet
,
LongKeyHashMap
Constructor and Description |
---|
LongHashSet()
Construct a new, empty HashSet whose backing HashMap has the default
capacity (11) and loadFacor (0.75).
|
LongHashSet(int initialCapacity)
Construct a new, empty HashSet whose backing HashMap has the supplied
capacity and the default load factor (0.75).
|
LongHashSet(int initialCapacity,
float loadFactor)
Construct a new, empty HashSet whose backing HashMap has the supplied
capacity and load factor.
|
LongHashSet(LongCollection c)
Construct a new HashSet with the same elements as are in the supplied
collection (eliminating any duplicates, of course).
|
Modifier and Type | Method and Description |
---|---|
boolean |
add(long v)
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(long v)
Returns true if the supplied element is in this Set.
|
boolean |
isEmpty()
Returns true if this set has no elements in it.
|
LongIterator |
iterator()
Returns an Iterator over the elements of this Set, which visits the
elements in no particular order.
|
boolean |
remove(long v)
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).
|
equals, hashCode, removeAll
addAll, containsAll, retainAll, toArray, toArray, toString
public LongHashSet()
public LongHashSet(int initialCapacity)
initialCapacity
- the initial capacity of the backing HashMapjava.lang.IllegalArgumentException
- if the capacity is negativepublic LongHashSet(int initialCapacity, float loadFactor)
initialCapacity
- the initial capacity of the backing HashMaploadFactor
- the load factor of the backing HashMapjava.lang.IllegalArgumentException
- if either argument is negative, or if loadFactor is
POSITIVE_INFINITY or NaNpublic LongHashSet(LongCollection c)
c
- a collection of initial set elementsjava.lang.NullPointerException
- if c is nullpublic boolean add(long v)
add
in interface LongCollection
add
in interface LongSet
add
in class AbstractLongCollection
v
- the Object to add to this Setpublic void clear()
clear
in interface LongCollection
clear
in interface LongSet
clear
in class AbstractLongCollection
LongIterator.remove()
public boolean contains(long v)
contains
in interface LongCollection
contains
in interface LongSet
contains
in class AbstractLongCollection
v
- the long to look forpublic boolean isEmpty()
isEmpty
in interface LongCollection
isEmpty
in interface LongSet
isEmpty
in class AbstractLongCollection
size() == 0
.AbstractLongCollection.size()
public LongIterator iterator()
ConcurrentModificationException
if the set is modified
externally.iterator
in interface LongCollection
iterator
in interface LongIterable
iterator
in interface LongSet
iterator
in class AbstractLongCollection
public boolean remove(long v)
remove
in interface LongCollection
remove
in interface LongSet
remove
in class AbstractLongCollection
v
- the long to removeLongIterator.remove()
public int size()
size
in interface LongCollection
size
in interface LongSet
size
in class AbstractLongCollection