E - the type of element stored in the setpublic class CompactHashSet<E> extends AbstractSet<E> implements Serializable
CompactHashSet implements the set interface more tightly in
memory and more efficiently than Java's HashSet.
This hash set allows arbitrary capacities sized hash sets to be created, including hash sets of size 0. When resizing is necessary due to objects being added, it resizes to the next largest capacity that is at least 1.5 times
HashSet?HashSet wraps a
HashMap, requiring a map entry with a dummy value
for each set entry. Map entries are even heavier as the map
entries also contain a next field to implement a linked list
structure to deal with collisions. This class represents hash set
entries in terms of the entries themselves. Iterators are based
directly on the underlying hash table.
Java's hash set implementation requires hash tables with capacities that are even powers of 2. This allows a very quick modulus operation on hash codes by using a mask, but restricts the size of hash sets to be powers of 2.
The current implementation uses linear probing with a step size of 1 for the case of hash code collisions. This means that if the position for an entry is full, the next position is considered (wrapping to the beginning at the end of the array).
We borrowed the supplemental hashing function from
HashMap (version ID 1.73). If the
initial hashFunction is h the supplemental
hash function is computed by:
static int supplementalHash(int n) {
int n2 = n ^ (n >>> 20) ^ (n >>> 12);
return n2 ^ (n2 >>> 7) ^ (n2 >>> 4);
}
This is required to scramble the hash code of strings
that share the same prefix with a different final character from
being adjacent. Recall that the hash code for strings s
consisting of characters s[0], ..., s[n-1] is
defined in String.hashCode() to be:
s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
Object.equals(Object) method may throw class cast exceptions
if the specified object is not of a type comparable to the elements
of the set.
As of this release, sets will never be resized downward.
Equality and Hash Codes
Compact hash sets satisfy the specification of the the equality metho,AbstractSet.equals(Object), and hash code
method AbstractSet.hashCode() specified by the Set interface.
The implementations are inherited from the superclass
AbstractSet, which uses the underlying iterators.
| Constructor and Description |
|---|
CompactHashSet(E... es)
Construct a compact hash set containing the specified list
of values.
|
CompactHashSet(int initialCapacity)
Construct a compact hash set with the specified initial
capacity.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
add(E e)
Add the specified object to the set if it is not already
present, returning true if the set didn't already
contain the element.
|
void |
clear()
Remove all of the elements from this set.
|
boolean |
contains(Object o)
Returns
true if this set contains the specified object. |
Iterator<E> |
iterator()
Returns an iterator over the elements in this set.
|
boolean |
remove(Object o)
Removes the specified object from the set, returning
true
if the object was present before the remove. |
boolean |
removeAll(Collection<?> collection)
Removes all the elements of the specified collection from this
set, returning
true if the set was modified as a
result. |
boolean |
retainAll(Collection<?> collection)
Remove all elements from this set that are not members of the
specified collection, returning
true if this set was
modified as a result of the operation. |
int |
size()
Returns the number of objects in this set.
|
Object[] |
toArray()
Returns an object array containing all of the members of this
set.
|
<T> T[] |
toArray(T[] array)
Returns an array of elements contained in this set, the runtime type
of which is that of the specified array argument.
|
equals, hashCodeaddAll, containsAll, isEmpty, toStringclone, finalize, getClass, notify, notifyAll, wait, wait, waitaddAll, containsAll, isEmpty, spliteratorparallelStream, removeIf, streampublic CompactHashSet(int initialCapacity)
IllegalArgumentException - If the capacity is less than 1.OutOfMemoryException - If the initial capacity is too
large for the JVM.public CompactHashSet(E... es)
es - Initial values to add to set.public boolean add(E e)
add in interface Collection<E>add in interface Set<E>add in class AbstractCollection<E>e - Object to add to set.true if the set didn't already contain the
element.NullPointerException - If the specified element is null.ClassCastException - If the class of this object prevents
it from being added to the set.public void clear()
Note that this operation does not affect the underlying capacity of the set itself.
clear in interface Collection<E>clear in interface Set<E>clear in class AbstractCollection<E>public boolean contains(Object o)
true if this set contains the specified object.contains in interface Collection<E>contains in interface Set<E>contains in class AbstractCollection<E>o - Object to test.true if this set contains the specified object.NullPointerException - If the specified object is null.ClassCastException - If the type of this object is incompatible
with this set.public Iterator<E> iterator()
Iterator.remove() operation,
which is not considered a concurrent modification.
Iteration order for sets is not guaranteed to be stable under adds or deletes, or for sets of different capacities containing the same elements.
The set must not be modified by other operations while an iteration is in process. Doing so may cause an illegal state and unpredictable behavior such as null pointer or array index exceptions.
public boolean remove(Object o)
true
if the object was present before the remove.remove in interface Collection<E>remove in interface Set<E>remove in class AbstractCollection<E>o - Object to remove.true if the object was present before the remove
operation.ClassCastException - If the specified object is not compatible
with this set.public boolean removeAll(Collection<?> collection)
true if the set was modified as a
result.
Implementation Note: Unlike the implementation the
parent class AbstractSet inherits from its
parent class AbstractCollection, this implementation
iterates over the argument collection, removing each of its
elements.
removeAll in interface Collection<E>removeAll in interface Set<E>removeAll in class AbstractSet<E>collection - Collection of objects to remove.true if the set was modified as a result.NullPointerException - If any of the collection members is null, or
if the specified collection is itself null.ClassCastException - If attempting to remove a member of the
collection results in a cast exception.public boolean retainAll(Collection<?> collection)
true if this set was
modified as a result of the operation.
Implementation Note: Unlike the implementation that
the parent class AbstractSet inherits from AbstractCollection, this implementation directly
visits the underlying hash entries rather than invoking the
overhead of an iterator.
retainAll in interface Collection<E>retainAll in interface Set<E>retainAll in class AbstractCollection<E>collection - Collection of objects to retain.true if this set was modified as a result of
the operation.ClassCastException - If comparing elements of the
specified collection to elements of this set throws a class
cast exception.NullPointerException - If the specified collection is
null.public int size()
size in interface Collection<E>size in interface Set<E>size in class AbstractCollection<E>public Object[] toArray()
The returned array is fresh and may be modified without affect this set.
toArray in interface Collection<E>toArray in interface Set<E>toArray in class AbstractCollection<E>public <T> T[] toArray(T[] array)
If the specified array argument is long enough to hold all
of the elements in this set, it will be filled starting from
index 0. If the specified array is longer than the size of the
set, the array entry following the last entry filled by this
set will be set to null.
If the specified array is not long enough to hold all of the elements, a new array will be created of the appropriate type through reflection.
toArray in interface Collection<E>toArray in interface Set<E>toArray in class AbstractCollection<E>T - Type of target array.array - Array of values determining type of output and containing
output elements if long enough.ArrayStoreException - If the members of this set cannot be
inserted into an array of the specified type.NullPointerException - If the specified array is null.Copyright © 2019 Alias-i, Inc.. All rights reserved.