Package host.anzo.commons.collection
Class ConcurrentHashSet<E>
java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractSet<E>
host.anzo.commons.collection.ConcurrentHashSet<E>
- Type Parameters:
E- the type of elements in this set
- All Implemented Interfaces:
Iterable<E>,Collection<E>,Set<E>
A thread-safe implementation of a Set using a ConcurrentHashMap.
This class allows for concurrent access and modification of the set
without the need for external synchronization.
- Since:
- 9/20/2021
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanAdds the specified element to this set if it is not already present.booleanaddIfAbsent(E o) Adds the specified element to this set if it is not already present.voidclear()Removes all the elements from this set.booleanReturns true if this set contains the specified element.booleanisEmpty()Returns true if this set contains no elements.iterator()Returns an iterator over the elements in this set.booleanRemoves the specified element from this set if it is present.intsize()Returns the number of elements in this set.Methods inherited from class java.util.AbstractSet
equals, hashCode, removeAllMethods inherited from class java.util.AbstractCollection
addAll, containsAll, retainAll, toArray, toArray, toStringMethods 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
addAll, containsAll, equals, hashCode, removeAll, retainAll, spliterator, toArray, toArray
-
Constructor Details
-
ConcurrentHashSet
public ConcurrentHashSet()Constructs a new, empty ConcurrentHashSet.
-
-
Method Details
-
size
public int size()Returns the number of elements in this set.- Specified by:
sizein interfaceCollection<E>- Specified by:
sizein interfaceSet<E>- Specified by:
sizein classAbstractCollection<E>- Returns:
- the number of elements in this set
-
iterator
Returns an iterator over the elements in this set. -
isEmpty
public boolean isEmpty()Returns true if this set contains no elements.- Specified by:
isEmptyin interfaceCollection<E>- Specified by:
isEmptyin interfaceSet<E>- Overrides:
isEmptyin classAbstractCollection<E>- Returns:
- true if this set is empty, false otherwise
-
add
Adds the specified element to this set if it is not already present.- Specified by:
addin interfaceCollection<E>- Specified by:
addin interfaceSet<E>- Overrides:
addin classAbstractCollection<E>- Parameters:
o- element to be added to this set- Returns:
- true if this set did not already contain the specified element
-
contains
Returns true if this set contains the specified element.- Specified by:
containsin interfaceCollection<E>- Specified by:
containsin interfaceSet<E>- Overrides:
containsin classAbstractCollection<E>- Parameters:
o- element whose presence in this set is to be tested- Returns:
- true if this set contains the specified element, false otherwise
-
clear
public void clear()Removes all the elements from this set.- Specified by:
clearin interfaceCollection<E>- Specified by:
clearin interfaceSet<E>- Overrides:
clearin classAbstractCollection<E>
-
remove
Removes the specified element from this set if it is present.- Specified by:
removein interfaceCollection<E>- Specified by:
removein interfaceSet<E>- Overrides:
removein classAbstractCollection<E>- Parameters:
o- element to be removed from this set, if present- Returns:
- true if this set contained the specified element
-
addIfAbsent
Adds the specified element to this set if it is not already present. This method is equivalent toadd(E)method, but may be more efficient in a concurrent context.- Parameters:
o- element to be added to this set- Returns:
- true if this set did not already contain the specified element
-