Package org.agrona.collections
Class LongHashSet
Open-addressing with linear-probing expandable hash set. Allocation free in steady state use when expanded.
By storing elements as long primitives this significantly reduces memory consumption compared with Java's builtin
HashSet<Long>. It implements Set<Long> for convenience, but calling
functionality via those methods can add boxing overhead to your usage.
This class is not Threadsafe.
This HashSet caches its iterator object by default, so nested iteration is not supported. You can override this behaviour at construction by indicating that the iterator should not be cached.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionfinal classIterator which supports unboxed access to the values viaLongHashSet.LongIterator.nextValue(). -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe initial capacity used when none is specified in the constructor. -
Constructor Summary
ConstructorsConstructorDescriptionConstruct a hash set withDEFAULT_INITIAL_CAPACITY,Hashing.DEFAULT_LOAD_FACTOR, iterator caching support and0as a missing value.LongHashSet(int proposedCapacity) Construct a hash set with a proposed capacity,Hashing.DEFAULT_LOAD_FACTOR, iterator caching support and0as a missing value.LongHashSet(int proposedCapacity, float loadFactor) Construct a hash set with a proposed initial capacity, load factor, iterator caching support and0as a missing value.LongHashSet(int proposedCapacity, float loadFactor, boolean shouldAvoidAllocation) Construct a hash set with a proposed initial capacity, load factor, iterator caching support and-1as a missing value. -
Method Summary
Modifier and TypeMethodDescriptionbooleanadd(long value) Primitive specialised overload of {this#add(Long)}.booleanbooleanaddAll(Collection<? extends Long> coll) booleanaddAll(LongHashSet coll) Alias foraddAll(Collection)for the specialized case when adding another LongHashSet, avoids boxing and allocations.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 coll) LongHashSet specialised variant of {this#containsAll(Collection)}.voidcopy(LongHashSet that) Copy values from anotherLongHashSetlongo this one.difference(LongHashSet other) Fast Path set difference for comparison with another LongHashSet.booleanvoidforEachLong(LongConsumer action) Iterate over the collection without boxing.inthashCode()booleanisEmpty()iterator()floatGet the load factor beyond which the set will increase size.booleanremove(long value) Specialised version of {this#remove(Object)} for long.booleanbooleanremoveAll(Collection<?> coll) booleanremoveAll(LongHashSet coll) Alias forremoveAll(Collection)for the specialized case when removing another LongHashSet, avoids boxing and allocations.booleanbooleanremoveIfLong(LongPredicate filter) Removes all the elements of this collection that satisfy the given predicate.intGet the actual threshold which when reached the map will resize.booleanretainAll(Collection<?> coll) booleanretainAll(LongHashSet coll) Alias forretainAll(Collection)for the specialized case when retaining on another LongHashSet, avoids boxing and allocations.intsize()Object[]toArray()<T> T[]toArray(T[] a) toString()Methods inherited from class java.util.AbstractCollection
containsAllMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
parallelStream, stream, toArrayMethods inherited from interface java.util.Set
containsAll, 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:
-
-
Constructor Details
-
LongHashSet
public LongHashSet()Construct a hash set withDEFAULT_INITIAL_CAPACITY,Hashing.DEFAULT_LOAD_FACTOR, iterator caching support and0as a missing value. -
LongHashSet
public LongHashSet(int proposedCapacity) Construct a hash set with a proposed capacity,Hashing.DEFAULT_LOAD_FACTOR, iterator caching support and0as a missing value.- 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, load factor, iterator caching support and0as a missing value.- Parameters:
proposedCapacity- for the initial capacity of the set.loadFactor- to be used for resizing.
-
LongHashSet
public LongHashSet(int proposedCapacity, float loadFactor, boolean shouldAvoidAllocation) Construct a hash set with a proposed initial capacity, load factor, iterator caching support and-1as a missing value.- Parameters:
proposedCapacity- for the initial capacity of the set.loadFactor- to be used for resizing.shouldAvoidAllocation- should the iterator be cached to avoid further allocation.
-
-
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.
-
remove
- Specified by:
removein interfaceCollection<Long>- Specified by:
removein interfaceSet<Long>- Overrides:
removein classAbstractCollection<Long>
-
remove
public boolean remove(long value) Specialised version of {this#remove(Object)} for long.- 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 checked 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>
-
addAll
Alias foraddAll(Collection)for the specialized case when adding another LongHashSet, avoids boxing and allocations.- Parameters:
coll- containing the values to be added.- Returns:
trueif this set changed as a result of the call.
-
containsAll
LongHashSet specialised variant of {this#containsAll(Collection)}.- Parameters:
coll- long hash set to compare against.- Returns:
- true if every element in other is in this.
-
difference
Fast Path set difference for comparison with another LongHashSet.Note: garbage free in the identical case, allocates otherwise.
- Parameters:
other- the other set to subtract.- Returns:
- null if identical, otherwise the set of differences.
-
removeIf
-
removeIfLong
Removes all the elements of this collection that satisfy the given predicate.NB: Renamed from removeIf to avoid overloading on parameter types of lambda expression, which doesn't play well with type inference in lambda expressions.
- Parameters:
filter- which returnstruefor elements to be removed.- Returns:
trueif any elements were removed.
-
removeAll
- Specified by:
removeAllin interfaceCollection<Long>- Specified by:
removeAllin interfaceSet<Long>- Overrides:
removeAllin classAbstractSet<Long>
-
removeAll
Alias forremoveAll(Collection)for the specialized case when removing another LongHashSet, avoids boxing and allocations.- Parameters:
coll- containing the values to be removed.- Returns:
trueif this set changed as a result of the call.
-
retainAll
- Specified by:
retainAllin interfaceCollection<Long>- Specified by:
retainAllin interfaceSet<Long>- Overrides:
retainAllin classAbstractCollection<Long>
-
retainAll
Alias forretainAll(Collection)for the specialized case when retaining on another LongHashSet, avoids boxing and allocations.- Parameters:
coll- containing elements to be retained in this set.- Returns:
trueif this set changed as a result of the call.
-
iterator
-
forEachLong
Iterate over the collection without boxing.- Parameters:
action- to be taken for each element.
-
copy
Copy values from anotherLongHashSetlongo this one.- Parameters:
that- set to copy values from.
-
toString
- Overrides:
toStringin classAbstractCollection<Long>
-
toArray
public <T> T[] toArray(T[] 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>
-
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>
-