Class LongHashSet
- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- java.util.AbstractSet<Long>
-
- org.apache.activemq.artemis.utils.collections.LongHashSet
-
- All Implemented Interfaces:
Serializable,Iterable<Long>,Collection<Long>,Set<Long>
public class LongHashSet extends AbstractSet<Long> implements Serializable
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 differentMISSING_VALUEto account forLong.hashCode()being 0 for -1.- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description classLongHashSet.LongIteratorIterator which supports unboxed access to values.
-
Field Summary
Fields Modifier and Type Field Description static intDEFAULT_INITIAL_CAPACITYThe initial capacity used when none is specified in the constructor.static floatDEFAULT_LOAD_FACTOR
-
Constructor Summary
Constructors Constructor Description LongHashSet()Construct 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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanadd(long value)Primitive specialised overload of {this#add(Long)}booleanadd(Long value)booleanaddAll(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.booleancontains(Object value)booleancontainsAll(LongHashSet other)LongHashSet specialised variant of {this#containsAll(Collection)}.booleanequals(Object other)inthashCode()booleanisEmpty()LongHashSet.LongIteratoriterator()floatloadFactor()Get the load factor beyond which the set will increase size.booleanremove(long value)An int specialised version of {this#remove(Object)}.booleanremove(Object value)booleanremoveAll(Collection<?> coll)intresizeThreshold()Get the actual threshold which when reached the map will resize.intsize()Object[]toArray()<E> E[]toArray(E[] a)StringtoString()-
Methods inherited from class java.util.AbstractCollection
containsAll, retainAll
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArray
-
Methods inherited from interface java.util.Set
containsAll, retainAll, spliterator
-
-
-
-
Field Detail
-
DEFAULT_INITIAL_CAPACITY
public static final int DEFAULT_INITIAL_CAPACITY
The initial capacity used when none is specified in the constructor.- See Also:
- Constant Field Values
-
DEFAULT_LOAD_FACTOR
public static final float DEFAULT_LOAD_FACTOR
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
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 Detail
-
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
public boolean add(Long value)
- 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
public boolean remove(Object value)
- 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
public boolean contains(Object value)
- 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:
Collection.contains(Object)
-
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
public boolean addAll(Collection<? extends Long> coll)
- Specified by:
addAllin interfaceCollection<Long>- Specified by:
addAllin interfaceSet<Long>- Overrides:
addAllin classAbstractCollection<Long>
-
removeAll
public boolean removeAll(Collection<?> coll)
- Specified by:
removeAllin interfaceCollection<Long>- Specified by:
removeAllin interfaceSet<Long>- Overrides:
removeAllin classAbstractSet<Long>
-
iterator
public LongHashSet.LongIterator iterator()
-
toString
public String 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
public Object[] toArray()
- Specified by:
toArrayin interfaceCollection<Long>- Specified by:
toArrayin interfaceSet<Long>- Overrides:
toArrayin classAbstractCollection<Long>
-
containsAll
public boolean containsAll(LongHashSet other)
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
public boolean equals(Object other)
- 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>
-
-