Package org.apache.hadoop.hdfs.util
Class LightWeightHashSet<T>
java.lang.Object
org.apache.hadoop.hdfs.util.LightWeightHashSet<T>
- All Implemented Interfaces:
Iterable<T>,Collection<T>
- Direct Known Subclasses:
LightWeightLinkedSet
A low memory linked hash set implementation, which uses an array for storing
the elements and linked lists for collision resolution. This class does not
support null element.
This class is not thread safe.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final floatprotected static final floatprotected org.apache.hadoop.hdfs.util.LightWeightHashSet.LinkedElement<T>[]An internal array of entries, which are the rows of the hash table.protected static final intprotected intModification version for fail-fast.protected intThe size of the set (not the entry array). -
Constructor Summary
ConstructorsConstructorDescriptionLightWeightHashSet(int minCapacity) LightWeightHashSet(int initCapacity, float maxLoadFactor, float minLoadFactor) -
Method Summary
Modifier and TypeMethodDescriptionbooleanAdd given element to the hash table.booleanaddAll(Collection<? extends T> toAdd) All all elements in the collection.protected booleanAdd given element to the hash tablevoidclear()Clear the set.booleanCheck if the set contains given elementbooleancontainsAll(Collection<?> c) protected voidChecks if we need to expand, and expands if necessary.intReturn the current capacity (for testing).protected TgetContainedElem(int index, T key, int hashCode) Check if the set contains given element at given index.getElement(T key) Return the element in this set which is equal to the given key, if such an element exists.protected intgetIndex(int hashCode) Get index in the internal table for a given hash.booleanisEmpty()Check if the set is empty.iterator()pollAll()Remove all elements from the set and return them.pollN(int n) Remove and return n elements from the hashtable.T[]pollToArray(T[] array) Get array.length elements from the set, and put them into the array.voidprintDetails(PrintStream out) Print detailed information of this object.booleanRemove the element corresponding to the key.booleanremoveAll(Collection<?> c) protected org.apache.hadoop.hdfs.util.LightWeightHashSet.LinkedElement<T>removeElem(T key) Remove the element corresponding to the key, given key.hashCode() == index.booleanretainAll(Collection<?> c) protected voidChecks if we need to shrink, and shrinks if necessary.intsize()Return the number of stored elements.Object[]toArray()<U> U[]toArray(U[] a) toString()Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
equals, hashCode, parallelStream, removeIf, spliterator, stream, toArray
-
Field Details
-
DEFAULT_MAX_LOAD_FACTOR
protected static final float DEFAULT_MAX_LOAD_FACTOR- See Also:
-
DEFAUT_MIN_LOAD_FACTOR
protected static final float DEFAUT_MIN_LOAD_FACTOR- See Also:
-
MINIMUM_CAPACITY
protected static final int MINIMUM_CAPACITY- See Also:
-
entries
An internal array of entries, which are the rows of the hash table. The size must be a power of two. -
size
protected int sizeThe size of the set (not the entry array). -
modification
protected int modificationModification version for fail-fast.- See Also:
-
-
Constructor Details
-
LightWeightHashSet
public LightWeightHashSet(int initCapacity, float maxLoadFactor, float minLoadFactor) - Parameters:
initCapacity- Recommended size of the internal array.maxLoadFactor- used to determine when to expand the internal arrayminLoadFactor- used to determine when to shrink the internal array
-
LightWeightHashSet
public LightWeightHashSet() -
LightWeightHashSet
public LightWeightHashSet(int minCapacity)
-
-
Method Details
-
isEmpty
public boolean isEmpty()Check if the set is empty.- Specified by:
isEmptyin interfaceCollection<T>- Returns:
- true is set empty, false otherwise
-
getCapacity
public int getCapacity()Return the current capacity (for testing). -
size
public int size()Return the number of stored elements.- Specified by:
sizein interfaceCollection<T>
-
getIndex
protected int getIndex(int hashCode) Get index in the internal table for a given hash. -
contains
Check if the set contains given element- Specified by:
containsin interfaceCollection<T>- Returns:
- true if element present, false otherwise.
-
getElement
Return the element in this set which is equal to the given key, if such an element exists. Otherwise returns null. -
getContainedElem
Check if the set contains given element at given index. If it does, return that element.- Returns:
- the element, or null, if no element matches
-
addAll
All all elements in the collection. Expand if necessary.- Specified by:
addAllin interfaceCollection<T>- Parameters:
toAdd- - elements to add.- Returns:
- true if the set has changed, false otherwise
-
add
Add given element to the hash table. Expand table if necessary.- Specified by:
addin interfaceCollection<T>- Returns:
- true if the element was not present in the table, false otherwise
-
addElem
Add given element to the hash table- Returns:
- true if the element was not present in the table, false otherwise
-
remove
Remove the element corresponding to the key.- Specified by:
removein interfaceCollection<T>- Returns:
- If such element exists, return true. Otherwise, return false.
-
removeElem
Remove the element corresponding to the key, given key.hashCode() == index.- Returns:
- If such element exists, return true. Otherwise, return false.
-
pollN
Remove and return n elements from the hashtable. The order in which entries are removed is unspecified, and and may not correspond to the order in which they were inserted.- Returns:
- first element
-
pollAll
Remove all elements from the set and return them. Clear the entries. -
pollToArray
Get array.length elements from the set, and put them into the array. -
shrinkIfNecessary
protected void shrinkIfNecessary()Checks if we need to shrink, and shrinks if necessary. -
expandIfNecessary
protected void expandIfNecessary()Checks if we need to expand, and expands if necessary. -
iterator
-
toString
-
printDetails
Print detailed information of this object. -
clear
public void clear()Clear the set. Resize it to the original capacity.- Specified by:
clearin interfaceCollection<T>
-
toArray
- Specified by:
toArrayin interfaceCollection<T>
-
toArray
public <U> U[] toArray(U[] a) - Specified by:
toArrayin interfaceCollection<T>
-
containsAll
- Specified by:
containsAllin interfaceCollection<T>
-
removeAll
- Specified by:
removeAllin interfaceCollection<T>
-
retainAll
- Specified by:
retainAllin interfaceCollection<T>
-