Class LongHashSet
- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- java.util.AbstractSet<java.lang.Long>
-
- org.apache.activemq.artemis.utils.collections.LongHashSet
-
- All Implemented Interfaces:
java.io.Serializable,java.lang.Iterable<java.lang.Long>,java.util.Collection<java.lang.Long>,java.util.Set<java.lang.Long>
public class LongHashSet extends java.util.AbstractSet<java.lang.Long> implements java.io.SerializableA 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(java.lang.Long value)booleanaddAll(java.util.Collection<? extends java.lang.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(java.lang.Object value)booleancontainsAll(LongHashSet other)LongHashSet specialised variant of {this#containsAll(Collection)}.booleanequals(java.lang.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(java.lang.Object value)booleanremoveAll(java.util.Collection<?> coll)intresizeThreshold()Get the actual threshold which when reached the map will resize.intsize()java.lang.Object[]toArray()<E> E[]toArray(E[] a)java.lang.StringtoString()
-
-
-
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(java.lang.Long value)
- Specified by:
addin interfacejava.util.Collection<java.lang.Long>- Specified by:
addin interfacejava.util.Set<java.lang.Long>- Overrides:
addin classjava.util.AbstractCollection<java.lang.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:
java.lang.IllegalArgumentException- if value is missingValue
-
remove
public boolean remove(java.lang.Object value)
- Specified by:
removein interfacejava.util.Collection<java.lang.Long>- Specified by:
removein interfacejava.util.Set<java.lang.Long>- Overrides:
removein classjava.util.AbstractCollection<java.lang.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(java.lang.Object value)
- Specified by:
containsin interfacejava.util.Collection<java.lang.Long>- Specified by:
containsin interfacejava.util.Set<java.lang.Long>- Overrides:
containsin classjava.util.AbstractCollection<java.lang.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 interfacejava.util.Collection<java.lang.Long>- Specified by:
sizein interfacejava.util.Set<java.lang.Long>- Specified by:
sizein classjava.util.AbstractCollection<java.lang.Long>
-
isEmpty
public boolean isEmpty()
- Specified by:
isEmptyin interfacejava.util.Collection<java.lang.Long>- Specified by:
isEmptyin interfacejava.util.Set<java.lang.Long>- Overrides:
isEmptyin classjava.util.AbstractCollection<java.lang.Long>
-
clear
public void clear()
- Specified by:
clearin interfacejava.util.Collection<java.lang.Long>- Specified by:
clearin interfacejava.util.Set<java.lang.Long>- Overrides:
clearin classjava.util.AbstractCollection<java.lang.Long>
-
addAll
public boolean addAll(java.util.Collection<? extends java.lang.Long> coll)
- Specified by:
addAllin interfacejava.util.Collection<java.lang.Long>- Specified by:
addAllin interfacejava.util.Set<java.lang.Long>- Overrides:
addAllin classjava.util.AbstractCollection<java.lang.Long>
-
removeAll
public boolean removeAll(java.util.Collection<?> coll)
- Specified by:
removeAllin interfacejava.util.Collection<java.lang.Long>- Specified by:
removeAllin interfacejava.util.Set<java.lang.Long>- Overrides:
removeAllin classjava.util.AbstractSet<java.lang.Long>
-
iterator
public LongHashSet.LongIterator iterator()
- Specified by:
iteratorin interfacejava.util.Collection<java.lang.Long>- Specified by:
iteratorin interfacejava.lang.Iterable<java.lang.Long>- Specified by:
iteratorin interfacejava.util.Set<java.lang.Long>- Specified by:
iteratorin classjava.util.AbstractCollection<java.lang.Long>
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.util.AbstractCollection<java.lang.Long>
-
toArray
public <E> E[] toArray(E[] a)
- Specified by:
toArrayin interfacejava.util.Collection<java.lang.Long>- Specified by:
toArrayin interfacejava.util.Set<java.lang.Long>- Overrides:
toArrayin classjava.util.AbstractCollection<java.lang.Long>
-
toArray
public java.lang.Object[] toArray()
- Specified by:
toArrayin interfacejava.util.Collection<java.lang.Long>- Specified by:
toArrayin interfacejava.util.Set<java.lang.Long>- Overrides:
toArrayin classjava.util.AbstractCollection<java.lang.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(java.lang.Object other)
- Specified by:
equalsin interfacejava.util.Collection<java.lang.Long>- Specified by:
equalsin interfacejava.util.Set<java.lang.Long>- Overrides:
equalsin classjava.util.AbstractSet<java.lang.Long>
-
hashCode
public int hashCode()
- Specified by:
hashCodein interfacejava.util.Collection<java.lang.Long>- Specified by:
hashCodein interfacejava.util.Set<java.lang.Long>- Overrides:
hashCodein classjava.util.AbstractSet<java.lang.Long>
-
-