Class LongHashSet
- All Implemented Interfaces:
Serializable,Iterable<Long>,Collection<Long>,Set<Long>
A hash set implementation of Set<Long> that uses open addressing values. To minimize the memory footprint,
this class uses open addressing rather than chaining. Collisions are resolved using linear probing. Deletions
implement compaction, so cost of remove can approach O(N) for full maps, which makes a small loadFactor recommended.
The implementation is based on Agrona
IntHashSet but uses long primitive keys and a different MISSING_VALUE to account for
Long.hashCode() being 0 for -1.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionfinal classIterator which supports unboxed access to values. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe initial capacity used when none is specified in the constructor.static final float -
Constructor Summary
ConstructorsConstructorDescriptionConstruct a hash set withDEFAULT_INITIAL_CAPACITYandDEFAULT_LOAD_FACTOR.LongHashSet(int proposedCapacity) Construct a hash set with a proposed capacity andDEFAULT_LOAD_FACTOR.LongHashSet(int proposedCapacity, float loadFactor) Construct a hash set with a proposed initial capacity and load factor. -
Method Summary
Modifier and TypeMethodDescriptionbooleanadd(long value) Primitive specialised overload of {this#add(Long)}booleanbooleanaddAll(Collection<? extends Long> coll) intcapacity()Get the total capacity for the set to which the load factor with be a fraction of.voidclear()voidcompact()Compact the backing arrays by rehashing with a capacity just larger than current size and giving consideration to the load factor.booleancontains(long value) Contains method that does not box values.booleanbooleancontainsAll(LongHashSet other) LongHashSet specialised variant of {this#containsAll(Collection)}.booleaninthashCode()booleanisEmpty()iterator()floatGet the load factor beyond which the set will increase size.booleanremove(long value) An int specialised version of {this#remove(Object)}.booleanbooleanremoveAll(Collection<?> coll) intGet the actual threshold which when reached the map will resize.intsize()Object[]toArray()<E> E[]toArray(E[] a) toString()Methods inherited from class java.util.AbstractCollection
containsAll, retainAllMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArrayMethods inherited from interface java.util.Set
containsAll, retainAll, spliterator
-
Field Details
-
DEFAULT_INITIAL_CAPACITY
public static final int DEFAULT_INITIAL_CAPACITYThe initial capacity used when none is specified in the constructor.- See Also:
-
DEFAULT_LOAD_FACTOR
public static final float DEFAULT_LOAD_FACTOR- See Also:
-
-
Constructor Details
-
LongHashSet
public LongHashSet()Construct a hash set withDEFAULT_INITIAL_CAPACITYandDEFAULT_LOAD_FACTOR. -
LongHashSet
public LongHashSet(int proposedCapacity) Construct a hash set with a proposed capacity andDEFAULT_LOAD_FACTOR.- Parameters:
proposedCapacity- for the initial capacity of the set.
-
LongHashSet
public LongHashSet(int proposedCapacity, float loadFactor) Construct a hash set with a proposed initial capacity and load factor.- Parameters:
proposedCapacity- for the initial capacity of the set.loadFactor- to be used for resizing.
-
-
Method Details
-
loadFactor
public float loadFactor()Get the load factor beyond which the set will increase size.- Returns:
- load factor for when the set should increase size
-
capacity
public int capacity()Get the total capacity for the set to which the load factor with be a fraction of.- Returns:
- the total capacity for the set
-
resizeThreshold
public int resizeThreshold()Get the actual threshold which when reached the map will resize. This is a function of the current capacity and load factor.- Returns:
- the threshold when the map will resize
-
add
- Specified by:
addin interfaceCollection<Long>- Specified by:
addin interfaceSet<Long>- Overrides:
addin classAbstractCollection<Long>
-
add
public boolean add(long value) Primitive specialised overload of {this#add(Long)}- Parameters:
value- the value to add- Returns:
- true if the collection has changed, false otherwise
- Throws:
IllegalArgumentException- if value is missingValue
-
remove
- Specified by:
removein interfaceCollection<Long>- Specified by:
removein interfaceSet<Long>- Overrides:
removein classAbstractCollection<Long>
-
remove
public boolean remove(long value) An int specialised version of {this#remove(Object)}.- Parameters:
value- the value to remove- Returns:
- true if the value was present, false otherwise
-
compact
public void compact()Compact the backing arrays by rehashing with a capacity just larger than current size and giving consideration to the load factor. -
contains
- Specified by:
containsin interfaceCollection<Long>- Specified by:
containsin interfaceSet<Long>- Overrides:
containsin classAbstractCollection<Long>
-
contains
public boolean contains(long value) Contains method that does not box values.- Parameters:
value- to be check for if the set contains it.- Returns:
- true if the value is contained in the set otherwise false
- See Also:
-
size
public int size()- Specified by:
sizein interfaceCollection<Long>- Specified by:
sizein interfaceSet<Long>- Specified by:
sizein classAbstractCollection<Long>
-
isEmpty
public boolean isEmpty()- Specified by:
isEmptyin interfaceCollection<Long>- Specified by:
isEmptyin interfaceSet<Long>- Overrides:
isEmptyin classAbstractCollection<Long>
-
clear
public void clear()- Specified by:
clearin interfaceCollection<Long>- Specified by:
clearin interfaceSet<Long>- Overrides:
clearin classAbstractCollection<Long>
-
addAll
- Specified by:
addAllin interfaceCollection<Long>- Specified by:
addAllin interfaceSet<Long>- Overrides:
addAllin classAbstractCollection<Long>
-
removeAll
- Specified by:
removeAllin interfaceCollection<Long>- Specified by:
removeAllin interfaceSet<Long>- Overrides:
removeAllin classAbstractSet<Long>
-
iterator
-
toString
- Overrides:
toStringin classAbstractCollection<Long>
-
toArray
public <E> E[] toArray(E[] a) - Specified by:
toArrayin interfaceCollection<Long>- Specified by:
toArrayin interfaceSet<Long>- Overrides:
toArrayin classAbstractCollection<Long>
-
toArray
- Specified by:
toArrayin interfaceCollection<Long>- Specified by:
toArrayin interfaceSet<Long>- Overrides:
toArrayin classAbstractCollection<Long>
-
containsAll
LongHashSet specialised variant of {this#containsAll(Collection)}.- Parameters:
other- int hash set to compare against.- Returns:
- true if every element in other is in this
-
equals
- Specified by:
equalsin interfaceCollection<Long>- Specified by:
equalsin interfaceSet<Long>- Overrides:
equalsin classAbstractSet<Long>
-
hashCode
public int hashCode()- Specified by:
hashCodein interfaceCollection<Long>- Specified by:
hashCodein interfaceSet<Long>- Overrides:
hashCodein classAbstractSet<Long>
-