Class ZHashSet<T>
- Type Parameters:
T
- the type of elements maintained by this set.
- All Implemented Interfaces:
Serializable
,Cloneable
,Iterable<T>
,Collection<T>
,Set<T>
- Direct Known Subclasses:
Ztrings
public class ZHashSet<T> extends HashSet<T>
HashSet
.
The Java implementation of HashSet
is unsynchronized. This class extends the implementation to provide
synchronization.
- See Also:
- Serialized Form
-
Constructor Summary
Constructors Constructor Description ZHashSet()
Constructs a new, empty set; the backing HashSet instance has default initial capacity (16) and load factor (0.75). -
Method Summary
Modifier and Type Method Description boolean
add(T e)
Synchronized implementation ofHashSet.add(java.lang.Object)
.boolean
addAll(Collection c)
Synchronized implementation ofAbstractCollection.addAll(java.util.Collection)
.boolean
addTo(Collection<T> col)
Adds the elements of this set to the given collection.boolean
anyMatch(Predicate<? super T> predicate)
Returns whether any elements of this set match the provided predicate.void
clear()
Synchronized implementation ofHashSet.clear()
.boolean
contains(Object o)
Synchronized implementation ofHashSet.contains(java.lang.Object)
.boolean
containsAll(Collection c)
Synchronized implementation ofAbstractCollection.containsAll(java.util.Collection)
.void
forEach(Consumer<? super T> action)
Synchronized implementation ofIterable.forEach(java.util.function.Consumer)
.static Collection
intersection(Collection... cols)
Returns an intersection of all given collections.Collection<T>
intersection(Collection<T> c)
Returns a new collection containing an intersection between this set and another collection without affecting this set.boolean
isEmpty()
Synchronized implementation ofHashSet.isEmpty()
.void
lockRead()
Locks the set for reading.void
lockWrite()
Locks the set for writing.boolean
remove(Object o)
Synchronized implementation ofHashSet.remove(java.lang.Object)
.boolean
removeAll(Collection c)
Synchronized implementation ofAbstractSet.removeAll(java.util.Collection)
.boolean
removeIf(Predicate<? super T> filter)
Synchronized implementation ofCollection.removeIf(java.util.function.Predicate)
.boolean
retainAll(Collection c)
Synchronized implementation ofAbstractCollection.retainAll(java.util.Collection)
.int
size()
Synchronized implementation ofHashSet.size()
.T[]
toArray(Class<T> cl)
Converts this set to a simple array.ZHashSet<T>
union(Collection<T> c)
Returns a new set that contains the union of this set and the given collection.void
unlockRead()
Unlocks the set after reading.void
unlockWrite()
Unlocks the set after writing.a
-
Constructor Details
-
ZHashSet
public ZHashSet()Constructs a new, empty set; the backing HashSet instance has default initial capacity (16) and load factor (0.75).
-
-
Method Details
-
lockRead
public final void lockRead()Locks the set for reading.This method is used by the class internally to provide synchronization, but you can use it directly when you need to perform a synchronized bulk read operation on the set.
-
lockWrite
public final void lockWrite()Locks the set for writing.This method is used by the class internally to provide synchronization, but you can use it directly when you need to perform a synchronized bulk write operation on the set.
-
unlockRead
public final void unlockRead()Unlocks the set after reading.This method is used by the class internally, but you should use it directly if you use
lockRead()
directly. -
unlockWrite
public final void unlockWrite()Unlocks the set after writing.aThis method is used by the class internally, but you should use it directly if you use
lockWrite()
directly. -
add
Synchronized implementation ofHashSet.add(java.lang.Object)
. -
addAll
Synchronized implementation ofAbstractCollection.addAll(java.util.Collection)
.- Specified by:
addAll
in interfaceCollection<T>
- Specified by:
addAll
in interfaceSet<T>
- Overrides:
addAll
in classAbstractCollection<T>
- Parameters:
c
- collection containing elements to be added to this set.- Returns:
- true if this set changed as a result of the call.
-
remove
Synchronized implementation ofHashSet.remove(java.lang.Object)
. -
removeAll
Synchronized implementation ofAbstractSet.removeAll(java.util.Collection)
.- Specified by:
removeAll
in interfaceCollection<T>
- Specified by:
removeAll
in interfaceSet<T>
- Overrides:
removeAll
in classAbstractSet<T>
- Parameters:
c
- collection containing elements to be removed from this set.- Returns:
- true if this set changed as a result of the call.
-
removeIf
Synchronized implementation ofCollection.removeIf(java.util.function.Predicate)
.- Parameters:
filter
- a predicate which returnstrue
for elements to be removed- Returns:
true
if any elements were removed
-
retainAll
Synchronized implementation ofAbstractCollection.retainAll(java.util.Collection)
.- Specified by:
retainAll
in interfaceCollection<T>
- Specified by:
retainAll
in interfaceSet<T>
- Overrides:
retainAll
in classAbstractCollection<T>
- Parameters:
c
- collection containing elements to be retained in this set.- Returns:
- true if this set changed as a result of the call.
-
contains
Synchronized implementation ofHashSet.contains(java.lang.Object)
. -
containsAll
Synchronized implementation ofAbstractCollection.containsAll(java.util.Collection)
.- Specified by:
containsAll
in interfaceCollection<T>
- Specified by:
containsAll
in interfaceSet<T>
- Overrides:
containsAll
in classAbstractCollection<T>
- Parameters:
c
- collection to be checked for containment in this set.- Returns:
- true if this set contains all of the elements in the specified collection.
-
forEach
Synchronized implementation ofIterable.forEach(java.util.function.Consumer)
.- Parameters:
action
- The action to be performed for each element
-
isEmpty
public boolean isEmpty()Synchronized implementation ofHashSet.isEmpty()
. -
size
public int size()Synchronized implementation ofHashSet.size()
. -
clear
public void clear()Synchronized implementation ofHashSet.clear()
. -
intersection
Returns a new collection containing an intersection between this set and another collection without affecting this set.The type of the returned collection is the type of this set.
- Parameters:
c
- the collection to intersect with this set.- Returns:
- a new collection in the type of this set containing an intersection between this set and another collection without affecting this set.
-
intersection
Returns an intersection of all given collections.This static method gets as arguments a variable length list of collections. If the list is empty, the method returns
null
, otherwise it returns the intersection of all the collections. The type of the returned collection is the type of the first argument.- Parameters:
cols
- the list of collections to operate on.- Returns:
- a collection containing the intersection of all given collections, in the type of the first collection, or null if there are no arguments.
-
union
Returns a new set that contains the union of this set and the given collection.The method instantiates a new set in the type of this set and fills it with the union of this set and the given collection. The current set is not affected.
- Parameters:
c
- the collection to unite with this set.- Returns:
- a new set in the type of this set that contains the union of this set and the given collection.
-
addTo
Adds the elements of this set to the given collection.Synchronizes the access to this set and adds its elements to the given collection.
- Parameters:
col
- the collection to add to- Returns:
true
if the target collection changed as a result of the call
-
toArray
Converts this set to a simple array.Synchronizes the access to this set and converts it to a simple array.
- Parameters:
cl
- theClass
object representing the type of the set elements.- Returns:
- an array containing all of the elements in this set
-
anyMatch
Returns whether any elements of this set match the provided predicate.Returns whether any elements of this set match the provided predicate. The method synchronizes the set for reading before searching for a match.
- Parameters:
predicate
- a non-interfering, stateless predicate to apply to elements of this set- Returns:
true
if any elements of the set match the provided predicate, otherwisefalse
-