Package org.agrona.collections
Class ObjectHashSet<T>
java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractSet<T>
org.agrona.collections.ObjectHashSet<T>
- Type Parameters:
T- type of values stored in theSet
- All Implemented Interfaces:
Iterable<T>,Collection<T>,Set<T>
Open-addressing with linear-probing expandable hash set. Allocation free in steady state use when expanded.
Ability to be notified when resizing occurs so that appropriate sizing can be implemented.
Not Threadsafe.
This HashSet caches its iterator object by default, which can be overridden, 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 over the set which can be optionally cached to avoid allocation. -
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, and iterator caching support.ObjectHashSet(int proposedCapacity) Construct a hash set with a proposed initial capacity,Hashing.DEFAULT_LOAD_FACTOR, and iterator caching support.ObjectHashSet(int proposedCapacity, float loadFactor) Construct a hash set with a proposed initial capacity, load factor, and iterator caching support.ObjectHashSet(int proposedCapacity, float loadFactor, boolean shouldAvoidAllocation) Construct a hash set with a proposed initial capacity, load factor, and indicated iterator caching support. -
Method Summary
Modifier and TypeMethodDescriptionbooleanbooleanaddAll(Collection<? extends T> coll) booleanaddAll(ObjectHashSet<T> coll) Alias foraddAll(Collection)for the specialized case when adding another ObjectHashSet, 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.booleanbooleancontainsAll(Collection<?> coll) voidcopy(ObjectHashSet<T> that) Copy data from the providedObjectHashSetinto this one.difference(ObjectHashSet<T> other) Fast Path set difference for comparison with another ObjectHashSet.booleanvoidinthashCode()booleanisEmpty()iterator()floatGet the load factor beyond which the set will increase size.booleanbooleanremoveAll(Collection<?> coll) booleanremoveAll(ObjectHashSet<T> coll) Alias forremoveAll(Collection)for the specialized case when removing another ObjectHashSet, avoids boxing and allocations.voidresizeNotifier(IntConsumer resizeNotifier) Add a Consumer that will be called when the collection is re-sized.intGet the actual threshold which when reached the map will resize.intsize()toString()Methods inherited from class java.util.AbstractCollection
retainAll, toArray, toArrayMethods 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
retainAll, spliterator, toArray, toArray
-
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
-
ObjectHashSet
public ObjectHashSet()Construct a hash set withDEFAULT_INITIAL_CAPACITY,Hashing.DEFAULT_LOAD_FACTOR, and iterator caching support. -
ObjectHashSet
public ObjectHashSet(int proposedCapacity) Construct a hash set with a proposed initial capacity,Hashing.DEFAULT_LOAD_FACTOR, and iterator caching support.- Parameters:
proposedCapacity- for the initial capacity of the set.
-
ObjectHashSet
public ObjectHashSet(int proposedCapacity, float loadFactor) Construct a hash set with a proposed initial capacity, load factor, and iterator caching support.- Parameters:
proposedCapacity- for the initial capacity of the set.loadFactor- to be used for resizing.
-
ObjectHashSet
public ObjectHashSet(int proposedCapacity, float loadFactor, boolean shouldAvoidAllocation) Construct a hash set with a proposed initial capacity, load factor, and indicated iterator caching support.- 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.
-
resizeNotifier
Add a Consumer that will be called when the collection is re-sized.- Parameters:
resizeNotifier- IntConsumer containing the new resizeThreshold
-
add
- Specified by:
addin interfaceCollection<T>- Specified by:
addin interfaceSet<T>- Overrides:
addin classAbstractCollection<T>- Parameters:
value- the value to add.- Returns:
- true if the collection has changed, false otherwise.
- Throws:
NullPointerException- if the value is null.
-
remove
- Specified by:
removein interfaceCollection<T>- Specified by:
removein interfaceSet<T>- Overrides:
removein classAbstractCollection<T>- 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<T>- Specified by:
containsin interfaceSet<T>- Overrides:
containsin classAbstractCollection<T>
-
size
public int size()- Specified by:
sizein interfaceCollection<T>- Specified by:
sizein interfaceSet<T>- Specified by:
sizein classAbstractCollection<T>
-
isEmpty
public boolean isEmpty()- Specified by:
isEmptyin interfaceCollection<T>- Specified by:
isEmptyin interfaceSet<T>- Overrides:
isEmptyin classAbstractCollection<T>
-
clear
public void clear()- Specified by:
clearin interfaceCollection<T>- Specified by:
clearin interfaceSet<T>- Overrides:
clearin classAbstractCollection<T>
-
containsAll
- Specified by:
containsAllin interfaceCollection<T>- Specified by:
containsAllin interfaceSet<T>- Overrides:
containsAllin classAbstractCollection<T>
-
addAll
- Specified by:
addAllin interfaceCollection<T>- Specified by:
addAllin interfaceSet<T>- Overrides:
addAllin classAbstractCollection<T>
-
addAll
Alias foraddAll(Collection)for the specialized case when adding another ObjectHashSet, avoids boxing and allocations.- Parameters:
coll- containing the values to be added.- Returns:
trueif this set changed as a result of the call.
-
difference
Fast Path set difference for comparison with another ObjectHashSet.NB: garbage free in the identical case, allocates otherwise.
- Parameters:
other- the other set to subtract.- Returns:
- null if identical, otherwise the set of differences.
-
removeAll
- Specified by:
removeAllin interfaceCollection<T>- Specified by:
removeAllin interfaceSet<T>- Overrides:
removeAllin classAbstractSet<T>
-
removeAll
Alias forremoveAll(Collection)for the specialized case when removing another ObjectHashSet, avoids boxing and allocations.- Parameters:
coll- containing the values to be removed.- Returns:
trueif this set changed as a result of the call.
-
iterator
-
copy
Copy data from the providedObjectHashSetinto this one.- Parameters:
that- set to copy data from.
-
toString
- Overrides:
toStringin classAbstractCollection<T>
-
equals
- Specified by:
equalsin interfaceCollection<T>- Specified by:
equalsin interfaceSet<T>- Overrides:
equalsin classAbstractSet<T>
-
hashCode
public int hashCode()- Specified by:
hashCodein interfaceCollection<T>- Specified by:
hashCodein interfaceSet<T>- Overrides:
hashCodein classAbstractSet<T>
-
forEach
-